The tiny BASH Cheet Sheet provides you (me) with a more or less alphabetically sorted tool-box of the expressions I most commonly use and tend to forget. I will update this cheat sheet now and then depending on more expressions I can’t remember :-)
Update [2019-07-12@21:09]: Added cheats on working with
args and provided some handy
functions + Update [2018-10-13@14:33]: Added some hints on
text conversions + Update [2018-09-22@16:37]: Added some information on
trap handlers and the debug “interrupt”.
$@is equivalent to
$2… which represent the positional parameters passed to your script. The variable
$#expands to the number of positional parameters in decimal (see also bash(1): GNU Bourne-Again SHell - Linux man page).
Test number of arguments
To test the number of arguments passed to your shell script, you can use the
This tests whether exactly one argument has been passed.
“… echo - display a line of text …” (from
Echo new lines
echo“the interpretation of the following backslash escapes”.
Don’t echo new lines
Echo to standard error
Echo to standard out
Echo to standard out and standard error
Echo ANSI escape codes
ANSI escape codes
can be used to output text in various colors:
\033[0mresets the escape codes being set before. The
echo“the interpretation of the following backslash escapes”. For more effects see also
“… A computer file is a computer resource for recording data discretely in a computer storage device. …” (see Computer file - Wikipedia)
Read all lines of a file
This one reads all lines of a file, including the last one not terminated with a new line:
“… Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a “regular” command…” (see Shell Functions (Bash Reference Manual))
Ask for continue/quit:
The below function asks whether to continue
c or to quit
q. The shell script will exit upon choosing
q or ask again till either
q is pressed:
Print separator line
The below function prints a separator line using the whole width of your terminal:
“… At times you need to specify different courses of action to be taken in a shell script, depending on the success or failure of a command …” (see Introduction to if - Bash Guide for Beginners
If on sub-string
Tests if a string begins with some substring:
If on empty variable
Tests whether a variable does not exist or is empty.
If on existing variable
Tests whether a variable exist and is not empty.
Control flow depending of the error code of a called command:
If on file exists
Test whether a file exists or not:
ifstatatement with the exclemation mark
if [ ! -f /tmp/foo.txt ]; ...
If on directory exists
Test whether a directory exists or not:
ifstatatement with the exclemation mark
if [ ! -d /home/nakatomi/tmp ]; ...
IFS stands for
internal field separator and is used by the shell to determine how to do word splitting and recognize word boundaries.
Elements by index
Get a sub-string from a string by its index given a known delimiter:
cutcommand is used to extract sections from lines of input.
Increment a value:
Remove last part of path
Given the path
This will result in the output
“… The read utility shall read a single line from standard input. …” (from
Halt till a key is pressed
You may halt the script till a key is pressed:
If on status code
Depending on the
shell you want to use it looks something like this at the beginning of your script file, being introduced with a
Convert text to lower case
Convert text to upper case
trap [-lp] [arg] [sigspec …]… The commands in
argare to be read and executed when the shell receives signal
sigspec…” (see Bash Reference Manual: Bourne Shell Builtins)
You may invoke your custom trap handler upon any occurrence of a
control signal such as
Traps can be used for advanced functionality such as the “debug” interrupt:
Upon execution of your
bash script, the
trap handler is called after each invocation of your script’s commands, with the variable
$BASH_COMMAND containing the
bashcommand to be executed next. You may halt the script till a key is pressed (
read -n 1 -s).
Replace the first occurrence of a string:
Replace all occurrences of a string: