This chapter contains tables of syntax elements for all three shells and examples of shell scripts.
Description | Bourne | Korn | C |
---|---|---|---|
Read at login. | .profile | .profile | .login |
Read at invocation of shell. | Any file specified in .profile with ENV=file. By convention, file is usually .kshrc | .cshrc |
Shell | Syntax |
---|---|
Bourne | #!/bin/sh |
Korn | #!/bin/ksh |
C | #!/bin/csh -f |
Operator | Description |
---|---|
${variable# pattern} | Delete the shortest part at the beginning of the variable that matches the pattern and return the rest. |
${variable## pattern} | Delete the longest part at the beginning of the variable that matches the pattern and return the rest. |
${variable% pattern} | Delete the shortest part at the end of the variable that matches the pattern and return the rest. |
${variable%% pattern} | Delete the longest part at the end of the variable that matches the pattern and return the rest. |
Modifier | Meaning | Description |
---|---|---|
:e | Extension | Remove prefix ending with a dot. |
:h | Head | Remove trailing path-name component. |
:r | Root | Remove suffixes beginning with a dot (.). |
:t | Tail | Remove all leading path-name components. |
:q | Quote | Force variable to be quoted. Used to quote $argv. |
:x | Quote | Like q, but break into words at each space, Tab, or newline. |
Variable | Explanation |
---|---|
$* | List the value of all command-line parameters. This variable is useful only in scripts because the login shell has no arguments associated with it. |
$# | Return the number of command-line arguments (in decimal). Useful only in scripts. |
$? | Return the exit status (in decimal) of the last command executed. Most commands return a zero exit status if they complete successfully; otherwise a non-zero exit status is returned. This variable is set after each command is executed. |
$$ | Return the process ID (PID) number of the current shell (in decimal). |
$! | Return the process number (in decimal) of the last process run in the background. |
Variable | Explanation |
---|---|
$* | List the value of all command-line parameters. This variable is useful only in scripts because the login shell has no arguments associated with it. Some people prefer to use $argv instead of $*. |
$# | Check whether a variable of that name has been set. |
The (K) in the Bourne or Korn Shell column indicates commands that are available only with the Korn shell.
Command | Description |
---|---|
<file, or 0< file | Take standard input from file. |
> file, or 1> file | Redirect STDOUT to file. |
2> file | Redirect STDERR to file. |
>> file | Append STDOUT to file. |
2>&1 | Redirect STDERR to the place where STDOUT is directed. |
cmd1 | cmd2 | Pipe standard output of cmd1 as standard input to cmd2. |
<> file | Use file as both STDIN and STDOUT. |
<&- | Close STDIN. |
>&- | Close STDOUT. |
2>&- | Close STDERR. |
Command | Description |
---|---|
> file | Redirect STDOUT to file. |
< file | Take input from file. |
>> file | Append STDOUT to end of file. |
>& file | Redirect STDOUT and STDERR to file. |
>>& file | Append STDOUT and STDERR to file. |
2>&- | Close STDERR. |
Notation | Description |
---|---|
$#argv | Count the number of command-line arguments. |
$* | Display all arguments. |
$argv | Display all arguments. |
$argv[1-3] | Display arguments 1 through 3. |
$0 | Display the command used to run the shell script. |
$argv[n] | Display the nth argument. |
$argv[$#argv} | Display the last argument. |
Feature | Bourne and Korn | C |
---|---|---|
Single-character wildcard. | ? | ? |
Any number of characters. | * | * |
Any one of the characters in the set of characters. | [abc] | [abc] |
Any one character in the range of characters. | [a-c] | [a-c] |
Any one character not in the range of characters specified. | [!a-c] | N/A |
Feature | Bourne | Korn | C |
---|---|---|---|
Assigning regular variables. | x=1 | x=1 | set x = 1 |
Accessing regular variables. | echo $x | echo $x | echo $x |
Assigning arrays. | N/A | y[0]=1; y[1]=2 | set y=(1 2) |
Accessing array elements. | N/A | echo $y echo ${y[1]} | echo $y[1] $y[2] |
Accessing entire array. | N/A | echo ${y[*]} | echo $y |
Exporting variables (make global). | export var | export var | setenv command |
Command-line arguments. | N/A | N/A | $argv, $#argv, $argv[1} |
Positional parameters. | $*, $#, $1 | $*, $#, $1 | $*, $1 |
Setting positional parameters. | set a b c | set a b c | N/A |
Feature | Bourne | Korn | C |
---|---|---|---|
STDOUT to file. | > filename or 1> filename | > filename or 1> filename | > filename |
STDIN from file. | < filename or 0< filename | < filename or 0< filename | < filename |
STDERR to file. | 2> filename | 2> filename | N/A |
Output and errors to file. | 2>&1 | 2>&1 | >& filename |
Output to next command. | | cmd | | cmd | | cmd |
Output and errors to next command. | 2>&1 | | 2>&1 | | |& |
Feature | Bourne | Korn | C |
---|---|---|---|
Display text and variables. | echo | print or echo | echo |
Feature | Bourne | Korn | C |
---|---|---|---|
Read keyboard input. | read name1 name2 . . . | read name1 name2 . . . | set var = $< |
Feature | Bourne | Korn | C |
---|---|---|---|
Perform a calculation. | var=`expr a + b` | let var= a + b | @ var = (a + b) |
Test a relational condition. | var=`expr a < b` | let var=a < b | @ var = (a < b) |
Feature | Bourne | Korn | C |
---|---|---|---|
Command substitution. | `command` | $(command) or `command` | `command` |
Feature | Korn | C |
---|---|---|
Tilde represents user's home directory. | ~loginid | ~ ~loginid |
Tilde represents current and previous directories. | ~+ ~- - | N/A |
Feature | Korn | C |
---|---|---|
Create new alias. | alias name=value | alias name value |
Display current list of values. | alias | alias |
Remove alias from list. | unalias name | unalias name |
Feature | Korn | C |
---|---|---|
Turn on history. | automatic | set history = num |
Display history list. | history or fc -l | history |
Display partial listing. | history n m history -n | history n |
Reexecute a command. | r string r number r | !string !number !! |
Feature | Bourne and Korn | C |
---|---|---|
Create a function. | func() {commands} function func {commands} | func() {commands} |
Use a function. | Use func as a command | Use func as a command |
Feature | Bourne and Korn | C |
if conditional. | if command | if (cond) then |
then commands elif command commands else commands fi |
commands else if (cond) then commands else commands endif | |
switch and case pattern. |
case variable in pattern) commands;; *) commands;; esac |
switch (variable) case pattern: commands default: commands endsw |
while loops. |
while command do commands done |
while (cond) commands end |
for/foreach loops. |
for variable in list do commands done |
foreach variable (list) commands end |
What Is Tested | Test Command | C Shell Built-In |
---|---|---|
file is block device. | -b file | N/A |
file is character device. | -c file | N/A |
file is directory. | -d file | -d file |
file or directory exists. | -e file (Korn shell only) | -e file |
file is a file. | -f file | -f file |
file has set-group-id bit set. | -g file | N/A |
file has sticky bit set. | -k file | N/A |
file is owned by executing user. | N/A | -o file |
file is a named pipe. | -p file | N/A |
Current user can read file. | -r file | -r file |
file exists and has size >0. | -s file | N/A |
n is a terminal file descriptor. | -t n | N/A |
file has set-user-id bit set. | -u file | N/A |
Current user can write to file. | -w file | -w file |
Current user can execute file. | -x file | N/A |
file has zero size. | N/A | -z file |
string is NULL. | -z string | string == "" |
string is NOT NULL. | string != "" | -n string, string |
strings are equal. | string = string | string == string |
strings are not equal. | string != string | string != string |
string matches file name wildcard pattern. | N/A | string =~ pattern |
string does not match file name wildcard pattern. | N/A | string !~ pattern |
num1 is equal to num2. | num1 -eq num2 | num1 == num2 |
num1 is not equal to num2. | num1 -ne num2 | num1 != num2 |
num1 is less than num2. | num1 -lt num2 | num1 < num2 |
num1 is less than or equal to num2. | num1 -le num2 | num1 <= num2 |
num1 is greater than num2. | num1 -gt num2 | num1 > num2 |
num1 is greater than or equal to num2. | num1 -ge num2 | num1 >= num2 |
Logical AND. | -a | && |
Logical OR. | -o | || |
Logical NEGATION. | ! | !! |
Operator | Description |
---|---|
+ | Addition. |
- | Subtraction. |
* | Multiplication. |
/ | Division. |
% | Remainder (modulus). |
Syntax | Description |
---|---|
@ variable = (expression) | Set value of variable equal to the expression. |
@ variable += (expression) | Addition. |
@ variable -= (expression) | Subtraction. |
@ variable *= (expression) | Multiplication. |
@ variable /= (expression) | Division. |
@ variable ++ | Add 1. |
@ variable -- | Subtract 1. |
3.129.21.47