Project 54. Understand Bash Internals"How does Bash's internal processing affect my command lines?" Learn More
This project explores the workings of the Bash shell and shows you how to avoid writing command lines that expand and process incorrectly. It highlights shell special characters that must be escaped on the command line and shows the order in which a command line is parsed and executed. Shell Special CharactersParticular characters have a special meaning to the Bash shell. If you wish to use them literally, they must be escaped (hidden from the shell) to prevent interpretation. We can do this in any of three ways:
Let's illustrate this with an example. We'll set up two shell variables and echo Jan owes me $20. We'll use double quotes, which escape all special characters except variable expansion, and then use backslash to escape the dollar symbol that should be taken literally. $ name="Jan"; amount=20 $ echo "*** $name owes me \$$amount" *** Jan owes me $20 Table 6.1 lists the shell special characters and their meaning.
Command Search OrderWhen Bash parses a command line, it interprets the first word as a command. That command may be an external Unix command, an alias, or perhaps both. Bash considers each possibility and picks the first that fits. The order in which Bash considers possible interpretations affects the meaning of the command line. The order is:
Three Bash built-in commands are available to override the natural order in which commands are considered. They are:
Command-Line ProcessingYou'll find it handy to know the order in which Bash evaluates and expands a command line, if only to understand why a particular command line might fail and how to correct it. Bash first reads a command line and tokenizes it, dividing it into elements like commands, variables, aliases, and strings. Processing then occurs in the following order:
Here are some examples that illustrate the significance of the command-line processing order. Learn More
Tokenization and alias expansion are repeated until no more aliases are found. Take advantage of this by defining an alias within an alias. In the example that follows, we define the alias lf, which includes in its definition the alias dir. $ alias lf=dir $ alias dir='ls -F' Alias lf will be expanded to dir and then alias dir will be expanded to ls -F. $ lf Desktop/ Library/ Music/ Public/ backup/ Documents/ Movies/ Pictures/ Sites/ Next, we demonstrate that aliases are expanded before tilde by virtue of the fact that the following example works. $ alias ld="ls ~/Documents" $ ld AppleWorks User Data Microsoft User Data iChats Downloads Sophie.ichat text However, tilde is expanded before variables, which is why the next example fails. $ doc="~/Documents" $ ls $doc ls: ~/Documents: No such file or directory Tip
The next two examples demonstrate that variables are expanded after aliases, and show what tricks will and will not work. (Aliases lf and dir from the example above must be defined.) $ alias hi='$Hello' $ Hello='echo "Hello there!"' $ hi "Hello there!" $ command='lf' $ $command -bash: lf: command not found Finally, we take advantage of the fact that variables are expanded before command substitution takes place. $ command="date" $ echo $($command) Thu Jul 28 11:03:03 BST 2005 |