Flylib.com

Books Software

 
 
 

Section 6.2. Invoking the Shell


6.2. Invoking the Shell

The command interpreter for the Bash shell ( bash ) or the Korn shell ( ksh ) can be invoked as follows :

bash  [


options


]  [


arguments


]

    ksh   [


options


]  [


arguments


]

ksh and Bash can execute commands from a terminal, from a file (when the first argument is an executable script), or from standard input (if no arguments remain or if -s is specified). Both shells automatically print prompts if standard input is a terminal, or if -i is given on the command line.

On Linux systems, /bin/sh is generally a link to Bash. When invoked as sh , Bash acts more like the traditional Bourne shell: Login shells read /etc/profile and ~/.profile , and regular shells read $ENV, if it's set. Full details are available on the bash (1) manpage .

6.2.1. Options

6.2.1.1 Common options


-c str

Read commands from string str .



-D

Print all $"..." strings in the program. Not ksh88 .



-i

Create an interactive shell (prompt for input).



-p

Start up as a privileged user . Bash: Don't read $ENV or $BASH_ENV, don't import functions from the environment, and ignore the value of $SHELLOPTS. Korn shell: Don't process $HOME/.profile , read /etc/suid_profile instead of $ENV.



-r

Create a restricted shell.



-s

Read commands from standard input; output from built-in commands goes to file descriptor 1; all other shell output goes to file descriptor 2.



- , --

End option processing.

6.2.1.2 Bash options


-O option

Enable shopt option option .



--debugger

Read the debugging profile at startup, turn on the extdebug option to shopt , and enable function tracing. For use by the Bash debugger.



--dump-po-strings

Same as -D , but output in GNU gettext format.



--dump-strings

Same as -D .



--help

Print a usage message and exit successfully.



--init-file file



--rcfile file

Use file as the startup file instead of ~/.bashrc for interactive shells.



--login

Shell is a login shell.



--noediting

Do not use the readline library for input, even in an interactive shell.



--noprofile

Do not read /etc/profile or any of the personal startup files.



--norc

Do not read ~/.bashrc . Enabled automatically when invoked as sh .



--posix

Turn on POSIX mode.



--restricted

Same as -r .



--verbose

Same as set -v ; the shell prints lines as it reads them.



--version

Print a version message and exit.

The remaining options to Bash and ksh are listed under the set built-in command.

6.2.2. Arguments

Arguments are assigned in order to the positional parameters $1 , $2 , etc. If the first argument is an executable script, commands are read from it, and the remaining arguments are assigned to $1 , $2 , etc. The name of the script is available as $0 .


6.3. Syntax

This section describes the many symbols peculiar to the Bash and Korn shells . The topics are arranged as follows :

  • Special files

  • Filename metacharacters

  • Quoting

  • Command forms

  • Redirection forms

  • Coprocesses (Korn shell only)

6.3.1. Special Files

Both shells read one or more startup files. Some of the files are read only when a shell is a login shell.

The Korn shell reads these files:

  1. /etc/profile . Executed automatically at login, first.

  2. ~/.profile . Executed automatically at login, second.

  3. $ENV. Specifies the name of a file to read when a new Korn shell is created. ( ksh88 : all shells, ksh93 : interactive shells only.) The value is a variable ( ksh93 : and command and arithmetic) substituted in order to determine the actual filename. Login shells read $ENV after processing /etc/profile and $HOME/.profile .

Bash reads these files:

  1. /etc/profile . Executed automatically at login, first.

  2. The first file found from this list: ~/.bash_profile , ~/.bash_login , or ~/.profile . Executed automatically at login, second.

  3. ~/.bashrc is read by every shell, after the login files. However, if invoked as sh , Bash instead reads $ENV, just as the Korn shell does.

For both shells, the getpwnam( ) and getpwuid( ) functions are are the sources of home directories for ~name abbreviations. (On single-user systems, the user database is stored in /etc/passwd . However on networked systems, this information may come from NIS, NIS+, or LDAPnot your workstation password file.)

6.3.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 specify a range (e.g., a-z , A-Z , 0-9 ).

[! abc ... ]

Match any character not enclosed as above.

~

Home directory of the current user.

~ name

Home directory of user name .

~+

Current working directory ($PWD).

~-

Previous working directory ($OLDPWD).


In the Korn shell, or Bash with the extglob option on:

Characters

Meaning

?( pattern )

Match zero or one instance of pattern .

*( pattern )

Match zero or more instances of pattern .

+( pattern )

Match one or more instances of pattern .

@( pattern )

Match exactly one instance of pattern .

!( pattern )

Match any strings that don't match pattern .

\ n

Match the text matched by the n th subpattern in (...) . ksh93 only.


This pattern can be a sequence of patterns separated by , meaning that the match applies to any of the patterns. This extended syntax resembles that available in egrep and awk . In the Korn shell, but not in Bash, if & is used instead of , all the patterns must match. & has higher precedence than .

ksh93 and Bash support the POSIX [[= c =]] notation for matching characters that have the same weight, and [[. c .]] for specifying collating sequences. In addition, character classes, of the form [[: class :]] , allow you to match the following classes of characters.

Class

Class

Class

Class

alnum

Alphanumeric characters

graph

Nonspace characters

alpha

Alphabetic characters

print

Printable characters

blank

Space or tab

punct

Punctuation characters

cntrl

Control characters

space

Whitespace characters

digit

Decimal digits

upper

Uppercase characters

lower

Lowercase characters

xdigit

Hexadecimal digits


Bash and ksh93 also accept the [:word:] character class, which is not in POSIX. [[:word:]] is equivalent to [[:alnum:]_] .

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

$

pr !(*.ocore)  lp


Print files that are not object files or core dumps


NOTE

On modern systems, ranges such as [D-R] are not portable; the system's locale may include more than just the uppercase letters from D to R in the range.

6.3.3. Quoting

Quoting disables a character's special meaning and allows it to be used literally, as itself. The following table displays characters that have special meaning to the Bash and Korn shells.

Character

Meaning

;

Command separator

&

Background execution

( )

Command grouping

Pipe

< > &

Redirection symbols

* ? [ ] ~ + - @ !

Filename metacharacters

" ' \

Used in quoting other characters

'

Command substitution

$

Variable substitution (or command or arithmetic substitution)

space tab newline

Word separators


These characters can be used for quoting:



" "

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



$

Variable (or command and arithmetic) substitution will occur.



'

Command substitution will occur.



"

This marks the end of the double quote.



' '

Everything between ' and ' is taken literally except for another ' . You cannot embed another ' within such a quoted string.



\

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



$" "

Not ksh88 . Just like " " , except that locale translation is done.



$' '

Not ksh88 . Similar to ' ' , but the quoted text is processed for the following escape sequences:

Sequence

Value

Sequence

Value

\a

Alert

\t

Tab

\b

Backspace

\v

Vertical tab

\c X

Control character X

\ nnn

Octal value nnn

\e

Escape

\x nn

Hexadecimal value nn

\E

Escape

\'

Single quote

\f

Form feed

\"

Double quote

\n

Newline

\\

Backslash

\r

Carriage return

   

6.3.3.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/bob
    $

echo "The value of $x is $x"

The value of $x is 100

6.3.4. Command Forms

Syntax

Effect

cmd &

Execute cmd in background.

cmd1 ; cmd2

Command sequence; execute multiple cmd s on the same line.

{ cmd1 ; cmd2 ; }

Execute commands as a group in the current shell.

( cmd1 ; cmd2 )

Execute commands as a group in a subshell.

cmd1 cmd2

Pipe; use output from cmd1 as input to cmd2 .

cmd1 ' cmd2 '

Command substitution; use cmd2 output as arguments to cmd1 .

cmd1 $( cmd2 )

POSIX shell command substitution; nesting is allowed.

cmd $(( expression ))

POSIX shell arithmetic substitution. Use the result of expression as argument to cmd .

cmd1 && cmd2

AND; execute cmd1 and then (if cmd1 succeeds) cmd2 . This is a " short-circuit " operation; cmd2 is never executed if cmd1 fails.

cmd1 cmd2

OR; execute either cmd1 or (if cmd1 fails) cmd2 . This is a "short-circuit" operation; cmd2 is never executed if cmd1 succeeds.

! cmd

NOT; execute cmd , and produce a zero exit status if cmd exits with a nonzero status. Otherwise, produce a nonzero status when cmd exits with a zero status. Not ksh88 .


6.3.4.1 Examples
$

nroff file > file.txt &


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 '(yesno)' `cat list`


Specify a list of files to search

$

egrep '(yesno)' $(cat list)


POSIX version of previous

$

egrep '(yesno)' $(< list)


Faster, not in POSIX

$

grep XX file && lp file


Print file if it contains the pattern;

$

grep XX file  echo "XX not found"


Otherwise, echo an error message


6.3.5. Redirection Forms

File descriptor

Name

Common abbreviation

Typical default

Standard input

stdin

Keyboard

1

Standard output

stdout

Screen

2

Standard error

stderr

Screen


{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

The usual input source or output destination can be changed, as seen in the following sections.

6.3.5.1 Simple redirection


cmd > file

Send output of cmd to file (overwrite).



cmd >> file

Send output of cmd to file (append).



cmd < file

Take input for cmd from file .



cmd << text

The contents of the shell script up to a line identical to text become the standard input for cmd ( text can be stored in a shell variable). This command form is sometimes called a Here document . Input is usually typed at the keyboard or in the shell program. Commands that typically use this syntax include cat , ex , and sed . (If <<- is used, leading tabs are stripped from the contents of the here document, and the tabs are ignored when comparing input with the end-of-input text marker.) If any part of text is quoted, the input is passed through verbatim. Otherwise, the contents are processed for variable, command, and arithmetic substitutions.



cmd <<< word

Supply text of word , with trailing newline, as input to cmd . (This is known as a here string , from the free version of the rc shell.) Not ksh88 .



cmd <> file

Open file for reading and writing on the standard input. The contents are not destroyed . [*]

[*] With < , the file is opened read-only, and writes on the file descriptor will fail. With <> , the file is opened read-write; it is up to the application to actually take advantage of this.



cmd > file

Send output of cmd to file (overwrite), even if the shell's noclobber option is set.

6.3.5.2 Redirection using file descriptors

Syntax

Effect

cmd >& n

Send cmd output to file descriptor n .

cmd m >& n

Same, except that output that would normally go to file descriptor m is sent to file descriptor n instead.

cmd >&-

Close standard output.

cmd <& n

Take input for cmd from file descriptor n .

cmd m <& n

Same, except that input that would normally come from file descriptor m comes from file descriptor n instead.

cmd <&-

Close standard input.

cmd <& n -

Move input file descriptor n instead of duplicating it. Not ksh88 .

cmd >& n -

Move output file descriptor n instead of duplicating it. Not ksh88 .


6.3.5.3 Multiple redirection

Syntax

Effect

cmd 2> file

Send standard error to file ; standard output remains the same (e.g., the screen).

cmd > file 2>&1

Send both standard error and standard output to file .

cmd &> file

Same. Bash only, preferred form.

cmd >& file

Same. Bash only.

cmd > f1 2> f2

Send standard output to file f1 , standard error to file f2 .

cmd tee files

Send output of cmd to standard output (usually the terminal) and to files . (See the Example in Chapter 3 under tee .)

cmd 2>&1 tee files

Send standard output and error output of cmd to standard output (usually the terminal) and to files .


No space should appear between file descriptors and a redirection symbol; spacing is optional in the other cases.

Bash allows multi-digit file descriptor numbers . The other shells do not.

6.3.5.4 Examples
$

cat part1 > book

$

cat part2 part3 >> book

$

mail tim < report

$

sed 's/^/XX /g' << 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 error messages (inaccessible files) to file no_access :

$

find / -print > filelist 2>no_access


6.3.6. Coprocesses

Coprocesses are a feature of the Korn shell only.

Syntax

Syntax

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

exec n <&p

Move input from coprocess to file descriptor n .

exec n >&p

Move output for coprocess to file descriptor n .


Moving the coprocess input and output file descriptors to standard file descriptors allows you to open multiple coprocesses.

6.3.6.1 Examples
$

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.