foo=bar
echo "$foo" # prints bar
echo '$foo' # prints $foo as literal string
$0
- Name of the script$1
to$9
- Arguments to the script. $1 is the first argument and so on.$@
- All the arguments should be quoted$*
- All the arguments as a single string; must be quoted$#
- Number of arguments$?
- Return code of the previous command$$
- PID for the current script$!
- PID of last job run in backgroundpossibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
!!
- Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions, then you can quickly execute it with sudo by doing sudo !!$_
- Last argument from the last command
if
case
while
for
- etc
mcd () {
mkdir -p "$1"
cd "$1" # also can use $_
}
- Commands will often return output using STDOUT, errors through STDERR and a Return Code to report errors in a more script friendly manner
- exit codes to conditionally execute commands
false || echo "Oops, fail" # false returns 1 to STD:ERR
true || echo "Will not be printed" # true returns 0 to STD:ERR
true && echo "Things went well"
false && echo "Will not be printed"
false ; echo "This will always run"
- Command substitution
$( CMD )
for file in $(ls)
- Process substitution
<( CMD )
diff <(ls foo) <(ls bar)
- Wildcards
?
,*
- Common substring
{}
- Expressions
( EXPRESSION )
-> EXPRESSION is true! EXPRESSION
-> EXPRESSION is falseEXPRESSION1 && EXPRESSION2
-> both EXPRESSION1 AND EXPRESSION2 are trueEXPRESSION1 || EXPRESSION2
-> either EXPRESSION1 OR EXPRESSION2 is true
- Strings
-n STRING
-> the length of STRING is nonzero-z STRING
-> the length of STRING is zeroSTRING1 = STRING2
-> the strings are equalSTRING1 = PATTERN
-> the string matches patternSTRING1 =~ REGEX
-> the string matches regexSTRING1 != STRING2
-> the strings are not equal
- Integers
INTEGER1 -eq INTEGER2
-> INTEGER1 is equal to INTEGER2INTEGER1 -ge INTEGER2
-> INTEGER1 is greater than or equal to INTEGER2INTEGER1 -gt INTEGER2
-> INTEGER1 is greater than INTEGER2INTEGER1 -le INTEGER2
-> INTEGER1 is less than or equal to INTEGER2INTEGER1 -lt INTEGER2
-> INTEGER1 is less than INTEGER2INTEGER1 -ne INTEGER2
-> INTEGER1 is not equal to INTEGER2
- Files
FILE1 -ef FILE2
-> FILE1 and FILE2 have the same device and inode numbersFILE1 -nt FILE2
-> FILE1 is newer (modification date) than FILE2FILE1 -ot FILE2
-> FILE1 is older than FILE2-b FILE
-> FILE exists and is block special-c FILE
-> FILE exists and is character special-d FILE
-> FILE exists and is a directory-e FILE
-> FILE exists-f FILE
-> FILE exists and is a regular file-g FILE
-> FILE exists and is set-group-ID-G FILE
-> FILE exists and is owned by the effective group ID-h FILE
-> FILE exists and is a symbolic link (same as -L)-k FILE
-> FILE exists and has its sticky bit set-L FILE
-> FILE exists and is a symbolic link (same as -h)-N FILE
-> FILE exists and has been modified since it was last read-O FILE
-> FILE exists and is owned by the effective user ID-p FILE
-> FILE exists and is a named pipe-r FILE
-> FILE exists and read permission is granted-s FILE
-> FILE exists and has a size greater than zero-S FILE
-> FILE exists and is a socket-t FD
-> file descriptor FD is opened on a terminal-u FILE
-> FILE exists and its set-user-ID bit is set-w FILE
-> FILE exists and write permission is granted-x FILE
-> FILE exists and execute (or search) permission is granted