Learning_the_bash_Shell_Third_Edition 2/n
#Special Characters and Quoting
Character Meaning See chapter ~ Home directory 1 ` Command substitution (archaic) 4 # Comment 4 $ Variable expression 3 & Background job 1 * String wildcard 1
Character Meaning See chapter ( Start subshell 8 ) End subshell 8 \ Quote next character 1 | Pipe 1 [ Start character-set wildcard 1 ] End character-set wildcard 1 { Start command block 7 } End command block 7 ; Shell command separator 3 ‘ Strong quote 1 " Weak quote 1 < Input redirect 1 > Output redirect 1 / Pathname directory separator 1 ? Single-character wildcard 1 ! Pipeline logical NOT 5
Quoting Quotation Marks
You can also use a backslash to include double quotes within a quoted string. For example:
$ echo \"2 \* 3 \> 5\" is a valid inequality.
produces the following output:
"2 * 3 > 5" is a valid inequality.
However, this won’t work with single quotes inside quoted expressions. For example, echo ‘Hatter\’s tea party’ will not give you Hatter’s tea party. You can get around this limitation in various ways. First, try eliminating the quotes:
$ echo Hatter\'s tea party
Control key stty name Function description CTRL-C intr Stop current command CTRL-D eof End of input CTRL-\ quit Stop current command if CTRL-C doesn’t work CTRL-S stop Halt output to screen CTRL-Q Restart output to screen DEL or CTRL-? erase Erase last character CTRL-U kill Erase entire command line CTRL-Z susp Suspend current command (see Chapter 8)
Enabling Command-Line Editing
bash initially starts interactively with emacs-mode as the default (unless you have started bash with the -noediting option; * see Chapter 10). There are two ways to enter either editing mode while in the shell. First, you can use the set command:
$ set -o emacs
or:
$ set -o vi
fc (short for: fix command)
The second way of selecting the editing mode is to set a readline variable in the file .inputrc.
The History List
Whenever you log in or start another interactive shell,bash reads an initial history list from the file .bash_history in your home directory.From that point on, every bash interactive session maintains its own list of commands. When you exit from a shell, it saves the list in .bash_history. You can call this file whatever you like by setting the environment variable HISTFILE.
The readline Startup File
backslash (\)
The default startup file is called .inputrc and must exist in your home directory if you wish to customize readline. You can change the default filename by setting the environment variable INPUTRC (see Chapter 3 for further information on environment variables). When bash starts up, it reads the startup file (if there is one) and any settings there come into effect. The startup file is just a sequence of lines that bind a keyname to a macro or readline function name. You can also place comments in the file by preceding any line with a #
As mentioned earlier, a vi user has the power to move mountains in few keystrokes—but at the cost of being unable to do anything meaningful in very few keystrokes.
Both bash editing modes have quite a few commands; you will undoubtedly develop keyboard habits that include just a few of them. If you use emacs-mode and you aren’t familiar with the full emacs, here is a subset that is easy to learn yet enables you to do just about anything:
• For cursor motion around a command line, stick to CTRL-A and CTRL-E for beginning and end of line, and CTRL-F and CTRL-B for moving around. • Delete using DEL (or whatever your “erase” key is) and CTRL-D; as with CTRL-F and CTRL-B, hold down to repeat if necessary. Use CTRL-K to erase the entire line. • Use CTRL-P and CTRL-N (or the up and down arrow keys) to move through the command history. • Use CTRL-R to search for a command you need to run again. • Use TAB for filename completion.
After a few hours spent learning these keystrokes, you will wonder how you ever got along without command-line editing.
Customizing Your Environment(CHAPTER 3)
Special files:
The files .bash_profile, .bash_logout, and .bashrc that are read by bash when you log in and out or start a new shell.
Aliases:
Synonyms for commands or command strings that you can define for convenience.
Options:
Controls for various aspects of your environment that you can turn on and off.
Variables:
Changeable values that are referred to by a name. The shell and other programs can modify their behavior according to the values stored in the variables.
The .bash_profile, .bash_logout, and .bashrc Files
These files may already exist in your home directory, depending on how your system administrator has set up your account. If they don’t exist, your account is using only the default system file /etc/profile. You can easily create your own bash files using your favorite text editor.
cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
If you examine your .bash_profile you will probably see lines similar to:
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin SHELL=/bin/bash MANPATH=/usr/man:/usr/X11/man EDITOR=/usr/bin/vi PS1='\h:\w\$ ' PS2='> ' export EDITOR
bash allows two synonyms for .bash_profile: .bash_login, derived from the C shell’s file named .login, and .profile, derived from the Bourne shell and Korn shell files named .profile. Only one of these three is read when you log in. If .bash_profile doesn’t exist in your home directory, then bash will look for .bash_login. If that doesn’t exist it will look for .profile.
.bash_profile is read and executed only by the login shell. If you start up a new shell(a subshell) by typing bash on the command line, it will attempt to read commands from the file .bashrc. This scheme allows you the flexibility to separate startup commands needed at login time from those you might need when you run a subshell. If you need to have the same commands run regardless of whether it is a login shell or a subshell, you can just use the source command from within .bash_profile to execute .bashrc. If .bashrc doesn’t exist then no commands are executed when you start up a subshell.
The file .bash_logout is read and executed every time a login shell exits. It is provided to round out the capabilities for customizing your environment. If you wanted to execute some commands that remove temporary files from your account or record how much time you have spent logged in to the system then you would place the commands in .bash_logout. This file doesn’t have to exist in your account—if it isn’t there when you log out, then no extra commands are executed.
Variables and Quoting
In Chapter 1, we said that some special characters inside double quotes are still interpreted, while none are interpreted inside single quotes.
But for now, we’ll revise the “When in doubt, use single quotes” rule in Chapter 1 by adding, “...unless a string contains a variable, in which case you should use double quotes.”