Command line interpretation

Command line interpretation is done in the following precedence:

1. History Substitution (not for bash)

2. Splitting words (based on whitespaces generally)

3. Updating the history list (not for bash)

4. Interpreting quotes (‘,”)

5. Alias substitution

6. Redirection of I/O (>,<,|)

7. Substitution of variables

8. Command substitution

9. Filename expansion

Consider a command to be executed:

ls -l $HOME/* | grep “bin”

For bash, the following steps are performed:

1. History substitution is NOT performed

2. The command is broken into words, delimited by the whitespaces. So the words formed are: ls, -l, $HOME/*, |, grep, “bin”

3. bash does not update history lists

4. Shell realises that “bin” is within quotes, so wildcard expansion will not be performed inside.

5. Shell checks if ‘ls’ and ‘grep’ are aliases.

6. Shell sets up everything required for catering to the |

7. Shell notices the presence of $HOME, and replaces it with the value of the environment variable. So the command now looks like:

ls -l /home/user/* | grep “bin”

8. Shell looks for any backquotes ( ` ). If it finds any the enclosed command is executed, and the output is inserted in the command.

9. All wildcards are expanded.

10. Shell executes the ‘ls’ command, and executes the ‘grep’ command, with the pipe redirecting the output of ls to the input of grep.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: