.
Tips For Linux

>> Wildcards, Quotes, Back Quotes and Apostrophes in shell commands ( * ? [] " ` ')

Now I shall discuss the use of special characters such as " ' and ` in various commands that you type at the shell prompt. The information given here is general and has to be followed when typing any command.


" " (Double Quotes) : Suppress Expansion


Whenever you use double quotes (" ") the shell suppress the filename expansion. Thus even if you use a wildcard such as * but enclose it within double quotes you would not get the standard feature of matching for all characters. I mean a command such as

$ ls c*
would list all the files with the names beginning with the letter ' c ' . But a command such as

$ ls "c*"
would search for a file named ' c* '. There would be no expansion of the * to match other letter sequences. The shell would expect the filename to have the actual character * in its name. Thus you would mostly get a 'No File Found error' message.

-

` (Back Quotes) : Command Substitution

The ` character (found on the key with the ~) is very important when used in shell commands. This ` indicates that command substitution is required wherever it is used. Hence whenever ` is used, whatever part of the command is enclosed by these Backquotes marks would be executed (as if it was the only command) and then the result of that command would be substituted in the original shell command that you typed. The following explains this clearly

$ echo "The contents of this directory are " `ls -l` > dir.txt

Note : Remember to use the ` (found on the key with the ~ and NOT the one found next to the Enter button)


The above command would basically execute the ' ls -l 'part first and then substitute the result after the string "The contents of this directory are " and both of these together (directory listing + the string) would be written to a file named dir.txt

Basically after command substitution the new substituted value would act as additional parameter to the main command that was present in your initial statement.

-

' (Apostrophe Marks) : No Change

The ' character (found on the button next to the Enter button) is a very powerful character whenever used in any shell command. Basically the ' (apostrophe marks) disables all kinds of transformations or modifications. It would consider whatever is enclosed with the ' marks as a single entity i.e. a single parameter. Absolutely no sort of substitution or expansion would take place.

$ echo '$HOME'
would produce at the output the string $HOME itself and would not print the path to your home directory. Since the single quotes prevents any sort of expansion, substitution and simple considers whatever to be present as a simple parameter in itself.

Just so that you remember in case you had typed the following

$ echo `$HOME`

(with the backquotes) you would get an error stating that the command not found. Since in this case the $HOME would be substituted with the path to your home directory (suppose /home/david) and the shell would try to execute the path as such. It would search for a program named (such as) /home/david. Remember that backquotes cause it to consider the part within the quotes to be considered as separate command and the output of that command would be substituted here. Hence in this case there is no command / program named as /home/david. Thus you would get an error when bash tries to execute that command

On the other hand when you type

$ echo "$HOME"
or $ echo $HOME
You would get the expected output. i.e the path to your home directory would be printed at the output.

Thus you are now familiar with forming various filenames using wildcards. You would generally end up using the special characters such as quotes when trying to make complex shell commands. With this knowledge I hope you can get the shell to do some real good stuff for you.

Goto Page 1 of this article

>
Tips For Linux © All Copyrights Reserved