This subsection describes the many symbols peculiar to bash. The topics are arranged as follows:
Special files
Filename metacharacters
Command-line editing
Quoting
Command forms
Redirection forms
Coprocesses
File | Purpose |
---|---|
/etc/profile | Executed automatically at login |
$HOME/.bash_profile | Executed automatically at login |
$HOME/.bashrc | Executed automatically at shell startup |
$HOME/.bash_logout | Executed automatically at logout |
$HOME/.bash_history | Record of last session's commands |
/etc/passwd | Source of home directories for ~name abbreviations |
Characters | Meaning |
---|---|
* | Match any string of zero or more characters. |
? | Match any single character. |
[abc...] | Match any one of the enclosed characters; a hyphen can be used to specify a range (e.g., a-z, A-Z, 0-9). |
[!abc...] | Match any character not among the enclosed characters. |
{str1,...} | Brace expansion: match any of the enclosed strings. |
~name | HOME directory of user name. |
~+ | Current working directory (PWD). |
~- | Previous working directory from directory stack (OLDPWD, see also the pushd built-in command). |
~+n | The nth entry in the directory stack, counting from the start of the list with the first entry being 0. |
~-n | The nth entry in the directory stack, counting from the end of the list with the last entry being 0. |
Patterns can be a sequence of patterns separated by |; if any of the subpatterns match, the entire sequence is considered matching. This extended syntax resembles that available to egrep and awk.
$ ls new* List new and new.1 $ cat ch? Match ch9 but not ch10 $ vi [D-R]* Match files that begin with uppercase D through R
Command lines can be edited like lines in either Emacs or vi. Emacs is the default. See Section 7.6.1, "Line-Edit Mode" later in this chapter for more information.
vi mode has two submodes, insert mode and command mode. The default mode is insert; you can go to command mode by pressing Esc. In command mode, typing a (append) or i (insert) will return you to insert mode.
Some users discover that the Del or Backspace key on the terminal does not delete the character before the cursor, as it should. Sometimes the problem can be solved by issuing one of the following commands (or placing it in your .bashrc file):
stty erase ^? stty erase ^H
See the stty command in Chapter 3, "Linux Commands" for more information. On the X Window System, an alternative solution is to use the xmodmap command, but this cannot be described easily here because it requires you to do some research about your terminal.
Table 7-1 through Table 7-14 show various Emacs and vi commands.
Command | Description |
Ctrl-B | Move backward one character (without deleting). |
Ctrl-F | Move forward one character. |
Del | Delete one character backward. |
Ctrl-D | Delete one character forward. |
Command | Description |
---|---|
Esc b | Move one word backward. |
Esc f | Move one word forward. |
Esc Del | Kill one word backward. |
Esc d | Kill one word forward. |
Ctrl-Y | Retrieve (yank) last item killed. |
Command | Description |
---|---|
Ctrl-A | Move to beginning of line. |
Ctrl-E | Move to end of line. |
Ctrl-K | Kill forward to end of line. |
Command | Description |
Ctrl-P | Move to previous line. |
Ctrl-N | Move to next line. |
Ctrl-R | Search backward. |
Esc < | Move to first line of history file. |
Esc > | Move to last line of history file. |
Command | Description |
---|---|
Tab | Attempt to perform general completion of the text. |
Esc ? | List the possible completions. |
Esc / | Attempt filename completion. |
Ctrl-X / | List the possible filename completions. |
Esc ~ | Attempt username completion. |
Ctrl-X ~ | List the possible username completions. |
Esc $ | Attempt variable completion. |
Ctrl-X $ | List the possible variable completions. |
Esc @ | Attempt hostname completion. |
Ctrl-X @ | List the possible hostname completions. |
Esc ! | Attempt command completion. |
Ctrl-X ! | List the possible command completions. |
Esc Tab | Attempt completion from previous commands in the history list. |
Command | Description |
Ctrl-J | Same as Return. |
Ctrl-L | Clear the screen, placing the current line at the top of the screen. |
Ctrl-M | Same as Return. |
Ctrl-O | Same as Return, then display next line in command history. |
Ctrl-T | Transpose character left of and under the cursor. |
Ctrl-U | Kill the line from the beginning to point. |
Ctrl-V | Insert keypress instead of interpreting it as a command. |
Ctrl-[ | Same as Esc (most keyboards). |
Esc c | Capitalize word under or after cursor. |
Esc u | Change word under or after cursor to all capital letters. |
Esc l | Change word under or after cursor to all lowercase letters. |
Esc . | Insert last word in previous command line after point. |
Esc _ | Same as Esc. |
Command | Description |
Del | Delete previous character. |
Ctrl-W | Erase previous word (i.e., erase until a blank). |
Ctrl-V | Insert keypress instead of interpreting it as a command. |
Esc | Enter control mode (see Table 7-8). |
Command | Description |
h | Move left one character. |
l | Move right one character. |
b | Move left one word. |
w | Move right one word. |
B | Move to beginning of preceding nonblank word. |
W | Move to beginning of next nonblank word. |
e | Move to end of current word. |
E | Move to end of current nonblank word. |
0 | Move to beginning of line. |
^ | Move to first nonblank character in line. |
$ | Move to end of line. |
Command | Description |
i | Insert text before current character (insert). |
a | Insert text after current character (append). |
I | Insert text at beginning of line. |
A | Insert text at end of line. |
r | Replace current character with this text. |
R | Overwrite existing text. |
Command | Description |
dh | Delete one character backward. |
dl | Delete one character forward. |
db | Delete one word backward. |
dw | Delete one word forward. |
dB | Delete one nonblank word backward. |
dW | Delete one nonblank word forward. |
d$ | Delete to end-of-line. |
d0 | Delete to beginning of line. |
Command | Description |
---|---|
D | Delete to end of line (equivalent to d$). |
dd | Delete entire line (equivalent to 0d$). |
C | Delete to end of line; enter input mode (equivalent to c$). |
cc | Delete entire line; enter input mode (equivalent to 0c$). |
X | Delete character backward (equivalent to dl). |
x | Delete character forward (equivalent to dh). |
Command | Description |
k or - | Move backward one line. |
j or + | Move forward one line. |
G | Move to line given by repeat count. |
/string | Search backward for string. |
?string | Search forward for string. |
n | Repeat search in same direction as previous. |
N | Repeat search in opposite direction of previous. |
Command | Description |
fx | Move right to next occurrence of x. |
Fx | Move left to previous occurrence of x. |
tx | Move right to next occurrence of x, then back one space. |
Tx | Move left to previous occurrence of x, then forward one space. |
; | Redo last character-finding command. |
, | Redo last character-finding command in opposite direction. |
Command | Description |
~ | Invert (toggle) case of current character(s). |
- | Append last word of previous command; enter input mode. |
Ctrl-L | Clear the screen and redraw the current line on it; good for when your screen becomes garbled. |
# | Prepend # (comment character) to the line and send it to the history file; useful for saving a command to be executed later, without having to retype it. |
Quoting disables a character's special meaning and allows it to be used literally, as itself. The following characters have special meaning to bash:
Character | Meaning |
---|---|
; | Command separator |
& | Background execution |
( ) | Command grouping (enter a subshell) |
{ } | Command block |
| | Pipe |
> < & | Redirection symbols |
* ? [ ] ~ ! | Filename metacharacters |
" ' \ | Used in quoting other characters |
` | Command substitution |
$ | Variable substitution (or command substitution) |
newline space tab |
Word separators |
# | Comment |
The following characters can be used for quoting:
Character | Action |
---|---|
" " | Everything between " and " is taken literally, except for the following characters that keep their special meaning:
|
' ' | Everything between ' and ' is taken literally, except for another '. |
\ | The character following a \ is taken literally. Use within " " to escape ", $, and |
$ echo 'Single quotes "protect" double quotes' Single quotes "protect" double quotes $ echo "Well, isn't that \"special\"?" Well, isn't that "special"? $ echo "You have` ls|wc -l` files in `pwd`" You have 43 files in /home/bob $ echo "The value of \$x is $x" The value of $x is 100
Syntax | Effect |
---|---|
cmd & | Execute cmd in background. |
cmd1 ; cmd2 | Command sequence; execute multiple cmds on the same line. |
(cmd1 ; cmd2) | Subshell; treat cmd1 and cmd2 as a command group. |
cmd1 | cmd2 | Pipe; use output from cmd1 as input to cmd2. |
cmd1 |
Command substitution; use cmd2 output as arguments to cmd1. |
cmd1 $(cmd2) | Command substitution; nesting is allowed. |
cmd1 && cmd2 | AND; execute cmd2 only if cmd1 succeeds. |
cmd1 || cmd2 | OR; execute cmd2 only if cmd1 fails. |
{ cmd1 ; cmd2 } | Execute commands in the current shell. |
$ nroff file & Format in the background $ cd; ls Execute sequentially $ (date; who; pwd) > logfile All output is redirected $ sort file | pr -3 | lp Sort file, page output, then print $ vi` grep -l ifdef *.c` Edit files found by grep $ egrep '(yes|no)'` cat list` Specify a list of files to search $ egrep '(yes|no)' $(cat list) Same as previous using bash command substitution $ egrep '(yes|no)' $(<list) Same, but faster $ grep XX file && lp file Print file if it contains the pattern $ grep XX file || echo "XX not found" Echo an error message if the pattern is not found
File Descriptor | Name | Common Abbreviation | Typical Default |
0 | Standard input | stdin | Keyboard |
1 | Standard output | stdout | Screen |
2 | Standard error | stderr | Screen |
The usual input source or output destination can be changed as shown in Table 7-15.
Redirector | Function |
---|---|
> file | Direct standard output to file. |
< file | Take standard input from file. |
cmd1 | cmd2 | Pipe; take standard output of cmd1 as standard input to cmd2. |
>> file | Direct standard output to file; append to file if it already exists. |
>| file | Force standard output to file even if noclobber is set. |
n>| file | Force output from the file descriptor n to file even if noclobber is set. |
<> file | Use file as both standard input and standard output. |
<< text | Read standard input up to a line identical to text (text can be stored in a shell variable). Input is usually typed on the screen or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is enclosed in quotes, standard input will not undergo variable substitution, command substitution, etc. |
n> file | Direct file descriptor n to file. |
n< file | Set file as file descriptor n. |
>&n | Duplicate standard output to file descriptor n. |
<&n | Duplicate standard input from file descriptor n. |
&>file | Direct standard output and standard error to file. |
<&- | Close the standard input. |
>&- | Close the standard output. |
n>&- | Close the output from file descriptor n. |
n<&- | Close the input from file descriptor n. |
$ cat part1 > book $ cat part2 part3 >> book $ mail tim < report $ grep Chapter part* 2> error_file $ sed 's/^/XX /' << END_ARCHIVE > This is often how a shell archive is "wrapped", > bundling text for distribution. You would normally > run sed from a shell program, not from the command line. > END_ARCHIVE XX This is often how a shell archive is "wrapped", XX bundling text for distribution. You would normally XX run sed from a shell program, not from the command line.
To redirect standard output to standard error:
$ echo "Usage error: see administrator" 1>&2
The following command sends output (files found) to filelist and sends error messages (inaccessible files) to file no_access:
$ find / -print > filelist 2>no_access
Coprocesses are a feature of bash and do not appear in other shells.
Syntax | Effect |
---|---|
cmd1 | cmd2 |& | Coprocess; execute the pipeline in the background. The shell sets up a two-way pipe, allowing redirection of both standard input and standard output. |
read -p var | Read coprocess input into variable var. |
print -p string | Write string to the coprocess. |
cmd <&p | Take input for cmd from the coprocess. |
cmd >&p | Send output of cmd to the coprocess. |
cat memo Print contents of file Sufficient unto the day is A word to the wise. ed - memo |& Start coprocess print -p /word/ Send ed command to coprocess read -p search Read output of ed command into variable search print "$search" Show the line on standard output A word to the wise.
Copyright © 2001 O'Reilly & Associates. All rights reserved.