Variable | Meaning |
---|---|
$* |
Positional parameters. Expands to a single word with spaces. |
$@ |
Positional parameters. Expands to separate words. |
$# |
The number of positional parameters |
$? |
Exit status of the most recently executed foreground pipeline |
$- |
Current option flags |
$$ |
Process ID of the shell |
$! |
Process ID of the most recently executed background (asynchronous) command |
$0 |
Name of the shell or shell script |
$_ |
Last argument(s) to the previous command |
Notes:
STRING
must be a variable that is prepended by an octothorpe (#
), not a dollar sign ($
)# The command has the form: SUB_STRING=${STRING:start:length}
STRING='Hello World'
SUBSTR=${STRING:0:5}
echo $SUBSTR # prints 'Hello'
SUBSTR=${STRING:3:5}
echo $SUBSTR # prints 'lo Wo'
SUBSTR=${STRING:6:-1}
echo $SUBSTR # prints 'Worl'
Notes:
STRING
must be a variable. It is NOT prepended by a dollar sign ($
)0
length
, can be negative. This is how many elements to be ignored at the end of STRING
Every variable in Bash is a string. To do math you need to surround the equations with $((
and ))
. Otherwise, operations like +
will keep their string behavior, in this case, concatenation.
If Then Else
Also called “Test Commands” or “Primaries”
Notes:
[
, ]
, [[
, and ]]
) and the EXPRESSION
.There are two different bracket types that be used to evaluate a Boolean expression, [ EXPRESSION ]
and [[ EXPRESSION ]]
. While these can be used interchangeably, they handle EXPRESSION
differently. The reason for this is historical. the single bracket, [
was developed in the Thompson shell, and is more limited than the double bracket, [[
, operator. The double bracket was introduced in the Korn shell and adopted by others (including Bash). It is an extension of the [
operator, and adds functions like pattern matching.
The general rules I follow are:
[
, when you are writing scripts that might be used on older systems, or needs to be run by a variety of shells.[ -n "$VARIABLE" ]
, (not [ -n $VARIABLE ]
.)Primary | Evaluates to |
---|---|
[ ARG1 -eq ARG2 ] |
True if ARG1 is equal to ARG2 |
[ ARG1 -ne ARG2 ] |
True if ARG1 is not equal to ARG2 |
[ ARG1 -lt ARG2 ] |
True if ARG1 is strictly less than ARG2 |
[ ARG1 -le ARG2 ] |
True if ARG1 is less than or equal to ARG2 |
[ ARG1 -gt ARG2 ] |
True if ARG1 is strictly greater than ARG2 |
[ ARG1 -ge ARG2 ] |
True if ARG1 is greater than or equal to ARG2 |
Primary | Evaluates to |
---|---|
[ -z STRING ] |
True of the length if STRING is zero. |
[ -n STRING ] |
True if the length of STRING is non-zero. |
[ STRING ] |
Same as above. |
[ STR1 == STR2 ] |
True if the strings are equal. |
[ STR1 = STR2 ] |
Same as above but is stritly POSIX compliant. |
[ STR1 != STR2 ] |
True if the strings are not equal. |
[ STR1 '<' STR2 ] |
True if STR1 sorts before STR2 lexicographically. |
[ STR1 '>' STR2 ] |
True if STR1 sorts after STR2 lexicographically. |
[ -o OPTIONNAME ] |
True if shell option OPTIONNAME is enabled. |
Primary | Evaluates to |
---|---|
[ -a FILE ] |
True if FILE exists |
[ -e FILE ] |
True if FILE exists |
[ -f FILE ] |
True if FILE exists and is a regular file |
[ -d FILE ] |
True if FILE exists and is a directory |
[ -L FILE ] |
True if FILE exists and is a symbolic link |
[ -h FILE ] |
True if FILE exists and is a symbolic link |
[ -p FILE ] |
True if FILE exists and is a named pipe (FIFO) |
[ -S FILE ] |
True if FILE exists and is a socket |
[ -s FILE ] |
True if FILE exists and has a size greater than zero |
[ -b FILE ] |
True if FILE exists and is a block-special file |
[ -c FILE ] |
True if FILE exists and is a character-special file |
[ -r FILE ] |
True if FILE exists and is readable |
[ -w FILE ] |
True if FILE exists and is writable |
[ -x FILE ] |
True if FILE exists and is executable |
[ -u FILE ] |
True if FILE exists and its SUID (set user ID) bit is set |
[ -g FILE ] |
True if FILE exists and its SGID bit is set |
[ -k FILE ] |
True if FILE exists and its sticky bit is set |
[ -O FILE ] |
True if FILE exists and is owned by the effective user ID |
[ -G FILE ] |
True if FILE exists and is owned by the effective group ID |
[ -t FD ] |
True if file descriptor FD is open and refers to a terminal |
[ -N FILE ] |
True if FILE exists and has been modified since it was last read |
[ FILE1 -nt FILE2 ] |
True if FILE1 has been changed more recently than FILE2 |
[ FILE1 -ot FILE2 ] |
True if FILE1 is older than FILE2 |
[ FILE1 -ef FILE2 ] |
True if FILE1 and FILE2 refer to the same device and inode numbers |
Arguments can be passed to functions as follows:
Arguments are accessed the same as in bash scripts:
$1
is the function name$2
is the first argument$3
is the second argument …return [INTEGER]
will stop function execution and will set $?
in the parent shell.exit
will stop execution and close the parent shell.Notes on Computers
AWS · Bash · C · C++ · Cyber Security · Git · LaTeX · Linux · Networking · Python · Raspberry Pi · Tools · Vim
Notes on Math & Physics
Information Theory · Linear Algebra · Solid State Physics
Copyright 2021 · Eric D. Weise