Built-in Commands:
trap
set
read
Redirection:
<&
>&
Programming Language:
for
case
while
if
Sequences:
[...]
Variables:
export
readonly
Startup:
.profile
Variables don’t have to be declared.
=
sign.VAR="Hello"
works, VAR = "Hello"
doesn’t work.Example
#!/bin/sh my_message="Hello World" echo $my_message
read
Lets you take input from stdin
.
Example
#!/bin/sh echo What is your name? read name echo "Hello $name- hope you're well.”
export
Lets you export the variable for it to be inherited by another program.
source
the script, which runs the script in your interactive shell instead of a non-interactive child shell.Example:
$ source ./.profile
Using curly braces for variables is a common convention in UNIX.
Form | Meaning |
---|---|
${variable?word} | Complain if undefined |
${variable-word} | Use new value if undefined |
${variable+word} | Opposite of the above |
${variable=word} | Use new value if undefined, and redefine |
Note: An empty string (
""
) counts as null.
Example:
$ d=`expr $d+1`
- Generates error if
$d
isn’t defined.$ d=`expr ${d-0}+1`
- Uses 0 if
$d
is defined.
unset
: Undefines variables.
Remember: The shell evaluates the variables and then operates on the results.
- e.g., Supposing
B
is undefined, thenA=$B
andA=
are the exact same thing.
$1
, $2,
...
, $9
: Positional parameters.shift
, which shifts all the variables to the left.$0
: Scriptname$*
: All positional parameters$@
: All positional parameters with spaces$#
: Number of parameters$$
: Current process ID$!
: ID of Background job$?
: error status$
: Set variablesNote: Limited range of positional parameters
$10
is equivalent to the value of the first argument ($1
) with a zero (0
) at the end of it.
${10}
doesn’t work either (though${10}
is supported in Bash and Korn).
Example: Using
shift
to move positional parameters.These two snippets assign the same positional variables to
arg*
:arg1=$1;shift; arg2=$1;shift; arg3=$1;shift;
arg1=$1 arg2=$2 arg3=$3
Example:
$*
#!/bin/sh # usage: shift.sh `date` echo $*;shift; echo $*;shift; echo $*;shift;
$ ./shift.sh `date` Mon Jan 8 07:26:57 PM PST 2024 Jan 8 07:26:57 PM PST 2024 8 07:26:57 PM PST 2024
Example:
$#
#!/bin/sh # usage: ./param_counter ... echo "Number of arguments are $6"
$ ./param_counter.sh 1 2 3 4 5 6 Number of arguments are 6
-x
: Flag for the Bourne shell that echoes every command being ran.
Ways to Turn it On:
set -x
and set x
to toggle it in the script body.#!/bin/sh -x
)$ -x ./script.sh
)expr
Four Types of Operations expr
Performs:
Operator | Type | Meaning |
---|---|---|
+ | Arithmetic | Addition |
- | Arithmetic | Subtraction |
* | Arithmetic | Multiplication |
/ | Arithmetic | Division |
% | Arithmetic | Remainder |
= | Relational | Equal to |
> | Relational | Greater Than |
>= | Relational | Greater Than or Equal to |
< | Relational | Less Than |
<= | Relational | Less than or Equal to |
!= | Relational | Not Equal to |
| | Boolean | Or |
& | Boolean | And |
: | String | Match or substitute |
Examples: Using
expr
$ echo `expr 2 + 5` 7 $ echo `expr length "cat"` 3 $ echo `expr substr "donkey" 4 3` key $ echo `expr index "donkey" "key"` 4
Control Structures:
if
… then
for
… in
while
until
case
break
and continue
Three Ways to Group Commands:
|
character.&
, ;
, &&
, or ||
; terminated by a semicolon, ampersand, or newline character.Example: Grouping commands
if list then list fi if list then list else list fi if list then list elif list then list fi if list then list elif list then list elif list then list fi if list then list elif list then list else list fi if list then list elif list then list elif list else list fi while list do list done until list do list done
Example: Using
case
#!/bin/sh while read f do case $f in hello) echo English ;; howdy) echo American ;; gday) echo Australian ;; bonjour) echo French ;; "guten tag") echo German ;; *) echo Unknown Language: $f ;; esac done < myfile
test
ExpressionsFormat:
[ expression ]
test
returns a zero exit code of the expression evaluates to true; a nonzero exit status otherwise.
Example
#!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" elif [ $a -gt $b ] then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "None of the condition met” fi
Use the function
keyword, like so:
#!/bin/sh
function square {
sq=`expr $1 \* $1`
echo "Number to be squared is $1."
echo "The result is $sq "
}
echo "Give me a number to square. "
read number
value_returned=`square $number`
echo $value_returned