Chapter 16 ■ telnet and SSh
292
To make the shell understand that you are talking about one file with a space in its name, not two files, you have
to contrive something like one of these possible command lines:
$ mv Smith\ Contract.txt ~/Documents
$ mv "Smith Contract.txt" ~/Documents
$ mv Smith*Contract.txt ~/Documents
The last possibility obviously means something different than the first two, since it will match any file name that
happens to start with Smith and end with Contract.txt regardless of whether the text between them is a simple space
character or a much longer sequence of text. I have often seen users resort to using a wildcard in frustration when they
are still learning shell conventions and cannot remember how to type a literal space character.
If you want to convince yourself that none of the characters that the bash shell has taught you to be careful about
is anything special, Listing 16-1 shows a simple shell written in Python that treats only the space character as special
but passes everything else through literally to the command.
Listing 16-1. Shell Supporting Whitespace-Separated Arguments
#!/usr/bin/env python3
Foundations of Python Network Programming, Third Edition
https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter16/shell.py
A simple shell, so you can try running commands at a prompt where no
characters are special (except that whitespace separates arguments).
import subprocess
def main():
while True:
args = input('] ').strip().split()
if not args:
pass
elif args == ['exit']:
break
elif args[0] == 'show':
print("Arguments:", args[1:])
else:
try:
subprocess.call(args)
except Exception as e:
print(e)
if name == 'main':
main()
Of course, the fact that this simple shell offers no special quoting characters means that you cannot use it to talk
about files with spaces in their names because it always, without exception, thinks that a space means the end of one
parameter and the beginning of the next.
By running this shell and trying all sorts of special characters of which you have been afraid to use, you can see
that they mean absolutely nothing if passed directly to the common commands you do use. (The shell in Listing 16-2
uses a ] prompt to make it easy to tell apart from your own shell.)