Bash Command Line Quoting

Part of the book: Bash: The Linux Command Line

If you've been following these articles or using the shell you know that there are several characters that have special meaning in the shell. For example you may have seen that the dollar sign $ is used to signify the use of a variable. You may also have seen the asterisk * used to specify a group of files, so-called file globbing. Even the space is a simple special character, it separates a command and it's arguments. These are only a few of the special characters, there are many more.

Special characters are interpreted by the shell before the arguments are passed to the command. This relieves each of the commands from the burden and allows the shell to provide consistency in the use of special characters.

There are times that we find special characters in file names or we want commands to see the special characters rather than have the shell interpret or remove the special commands. Fortunately the shell provides a way to do this, it's called quoting, and it uses three other special characters.

Double quotes

Double quotes can be used to disable only some of the special characters. It is useful in disabling all but the $, `, \ and ! characters. One basic way to look at this is that it tells the shell to ignore shell globbing and spaces. e.g:

rm "file name"

Single quotes is the most powerful of quotes removing the special meaning of all characters except the single quote. e.g.:

vi 'us$ account.txt'

Backslash can be used to quote a single character. We normally think that quotes need to surround a string, but in the case of the backslash since it quotes only a single character it is only needed before the character. The backslash can quote any character as long as it is not quoted by itself or a single quote. The only exception is the newline character, in this case the newline is ignored and not passed to the command. e.g.

cp us\$account.txt "us dollar account.txt"

Another method of quoting actually adds special characters. The $'' quote allows specification of many non-printable characters, like newline, bell, tab and others.

echo $'bing \a'

For additional variables to use in the $'' quote view the man page for bash (i.e. run man bash) and search for the QUOTING section.

Finally there is a $"" quoting method. This seems to be the same as the double quotes, except that it will change how it works to suit other languages.

Hyphens

Have you ever seen a filename that begins with a hyphen. These are often mistakes, but if you find one it can be hard to get rid of. When you try to use rm (e.g. rm -file the command will think that the -file are options and will give an error message. This happens with all sorts of other commands too.

The fix is easy and fairly consistent, use -- to separate options from arguments. To use this method supply all your options before the hyphened file then use -- and then the file name, e.g.

rm -r -- -file

Unprintable Characters

These can be another problem. Somehow, usually because of pressing a function key at the wrong time, a file is created that begins or contains a non-printable character. If the name only contains unprintable characters or doesn't include anything unique that a file glob can match then you have to resort to this trick.

First let's create a file that has the name of an escape character:

date > $'\033'

I used one of the quotes I mentioned above to create a file named with a char 27, the ASCII code for Esc. In octal 27 is 33.

Now if you use ls you'll see a file with an odd name. Here is how to identify it and delete it:

List the directory using the -b option, this will show non-printable characters as backslash codes:

ls -lb

You'll see a file apparently called \033, this is your "Esc" file. To delete it we'll use the same code:

rm $'033'

If the file only begins with that you can use a shell glob to handle the rest:

rm $'\033'*