Section 5.2. Syntax

5.2. Syntax

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

5.2.1. Special Files

Table 5-1. Special bash configuration files

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.

$HOME/.inputrc

Initialization file for reading input in an interactive shell.

/etc/passwd

Source of home directories for ~ name abbreviations.


5.2.2. Filename Metacharacters

Table 5-2. Filename metacharacters

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.

[^ abc ...]

Same as [! abc ... ].

{ str1 ,...}

Brace expansion: match any of the enclosed strings.

~ name

Home directory of user name . With no name , Home directory of current user.

~+

Current working directory (PWD).

~-

Previous working directory from directory stack (OLDPWD; see also the pushd built-in command).

~+ n

The n th entry in the directory stack, counting from the start of the list with the first entry being 0.

~- n

The n th 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 subpatterns match, the entire sequence is considered matching. This extended syntax resembles that of egrep and awk .

5.2.2.1. Examples
 $ 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 

5.2.3. Command-Line Editing

Command lines can be edited like lines in either Emacs or vi. Emacs is the default. See "Line-Edit Mode" later in this chapter for more information.

vi mode has two submodes, input mode and command mode . The default is input mode; you can go to command mode by pressing Esc. In command mode, typing a (append) or i (insert) returns you to input mode.

Some users discover that the Del or Backspace key in the Terminal doesn't delete the character before the cursor as it should. Sometimes this 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 2 for more information.

Emacs commands use the Control key and the Meta keya system-neutral way to describe a function-changing modifier key. On a Mac, this corresponds to the Option key, if you have the Terminal configured correctly. Under the Terminal menu, go to Settings, and select the Keyboard drop down. Check the "use option key as meta key" checkbox, and close the Terminal Inspector.

In this chapter, the notation C- indicates that the Control key is pressed at the same time as the character that follows. Similarly, M- indicates the use of the Meta, or Option key on Mac OS X: either hold Option while typing the next character, or press and release the Escape key followed by the next character.

Tables 5-3 through 5-16 show various Emacs and vi commands.

Table 5-3. Basic Emacs-mode 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.


Table 5-4. Emacs-mode word commands

Command

Description

M-b

Move one word backward.

M-f

Move one word forward.

M-Del

Kill one word backward.

M-d

Kill one word forward.

Ctrl-y

Retrieve ( yank ) last item killed .


Table 5-5. Emacs-mode line commands

Command

Description

Ctrl-a

Move to beginning of line.

Ctrl-e

Move to end of line.

Ctrl-k

Kill forward to end of line.


Table 5-6. Emacs-mode commands for moving through the history file

Command

Description

Ctrl-p

Move to previous command.

Ctrl-n

Move to next command.

Ctrl-r

Search backward.

M-<

Move to first line of history file.

M->

Move to last line of history file.


Table 5-7. Emacs-mode completion commands

Command

Description

Tab

Attempt to perform general completion of the text.

M-?

List the possible completions.

M-/

Attempt filename completion.

Ctrl-x /

List the possible filename completions.

M-~

Attempt username completion.

Ctrl-x ~

List the possible username completions.

M-$

Attempt variable completion.

Ctrl-x $

List the possible variable completions.

M-@

Attempt hostname completion.

Ctrl-x @

List the possible hostname completions.

M-!

Attempt command completion.

Ctrl-x !

List the possible command completions.

M-Tab

Attempt completion from previous commands in the history list.


Table 5-8. Miscellaneous Emacs-mode commands

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 next keypress literally instead of interpreting it as a command.

Ctrl-[

Same as Esc (most keyboards).

M-c

Capitalize word under or after cursor.

M-u

Change word under or after cursor to all capital letters .

M-l

Change word under or after cursor to all lowercase letters.

M-.

Insert last word in previous command line after point.

M-_

Same as M- . .


Table 5-9. Editing commands in vi input mode

Command

Description

Del

Delete previous character.

Ctrl-Shift-W

Erase previous word (i.e., erase until a blank).

Ctrl-Shift-V

Insert next keypress literally instead of interpreting it as a command.

Esc

Enter command mode (see Table 5-8).


Table 5-10. Basic vi command-mode commands

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.

Move to beginning of line.

^

Move to first nonblank character in line.

$

Move to end of line.


Table 5-11. Commands for entering vi input mode

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 next keypress.

R

Overwrite existing text.


Table 5-12. Some vi-mode delete commands

Command

Description

Dh

Delete one character backward.

Dl

Delete the current character.

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.


Table 5-13. Abbreviations for vi-mode delete commands

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 dh ).

X

Delete the current character (equivalent to dl .)


Table 5-14. vi-mode commands for searching the command history

Command

Description

k or -

Move backward one line.

j or +

Move forward one line.

G

Move to first line in history.

/ 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.


Table 5-15. vi-mode character-finding commands

Command

Description

f x

Move right to next occurrence of x .

F x

Move left to previous occurrence of x .

t x

Move right to next occurrence of x , then back one space.

T x

Move left to previous occurrence of x , then forward one space.

;

Redo last character-finding command.

,

Redo last character-finding command in opposite direction.


Table 5-16. Miscellaneous vi-mode commands

Command

Description

~

Invert (toggle) case of current character(s).

_

Insert last word of previous command after cursor; 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.


5.2.4. Quoting

Quoting disables a character's special meaning and allows it to be used literally, as itself. The following characters have special meaning to bash :

Table 5-17. Characters with special meaning in 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:

Table 5-18. Characters used for quoting in bash

Character

Action

""

Everything between " and " is taken literally, except for the following characters that keep their special meanings:



$

Variable substitution will occur.



'

Command substitution will occur.



"

This marks the end of the double quote.

''

Everything between 'and' is taken literally, except for another '.

\

The character following \ is taken literally. Use within " " to escape ", $, and '. Often used to escape itself, spaces, or newlines.


5.2.4.1. Examples
 $ 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/andy     $ x=100     $ echo "The value of $x is $x"     The value of $x is 100 

5.2.5. Command Forms

Table 5-19. bash command forms

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 ' cmd2 '

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.


5.2.5.1. Examples
 $ 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 '(yesno)' 'cat list'  Specify a list of files to search  $ egrep '(yesno)' $(cat list)  Same as previous using bash command                                  substitution  $ egrep '(yesno)' $(<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 pattern   not found  

5.2.6. Redirection Forms

Table 5-20. I/O file descriptors

File descriptor

Name

Common abbreviation

Typical default

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 5-21.

Table 5-21. I/O redirectors

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 .


5.2.6.1. Examples
 $ 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 



MAC OS X Tiger in a Nutshell
Mac OS X Tiger in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009437
EAN: 2147483647
Year: 2003
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net