📅 2020-Sep-17 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ bash, cheatsheet ⬩ 📚 Archive
Here is some information I find useful when using bash as a shell or when writing bash scripts.
History: Press Ctrl + R
to invoke searching through history. Start typing the text you want to search. Bash will show the best match to your string. Repeatedly press Ctrl + R
to cycle through the matches or continue typing more text to hone your search. When you have found the command you want to pick, press Enter
.
Get help at the shell:
$ man bash
$ help set
set
options that are currently enabled:$ echo $-
$
and enclose in double quotes:echo "$FOOBAR"
The double quoting is needed to handle the case of the variable value having whitespace. But to have reliable scripts, it is better to always double quote all reads of variables.
=
operator to assign to a variable:FOOBAR=1
FOOBAR=$(ls -l)
FOOBAR=${ANOTHER_VAR}
Remember to not use any spaces before and after the =
. Otherwise Bash will interpret the statement as a command.
local x # x will be local variable
x=$(FOOBAR)
If this declaration is done inside a function, the variable is local to that function.
# Same as $foobar
${foobar}
# If variable is not set or null, use a different value
${foobar:-"some other value"}
# If variable is not set or null, print your error message and exit
${foobar:?"Error: foobar is not set"}
# Get length of value of variable
${#foobar}
# Remove prefix pattern from value in variable
${foobar#"tmp_"}
# Remove suffix pattern from value in variable
${foobar%".tmp"}
# Search and replace first instance of string with another string in variable
${foobar/"find this"/"replace with this"}
# Search and replace all instances of string with another string in variable
${foobar//"find this"/"replace with this"}
# Get a substring of value in variable
${foobar:offset:len}
exit
function is invoked with a return value to quit the script:exit 1
exit $RETURN_VALUE
Use return value 0
on success and any non-zero value (typically 1
) to indicate failure.
cmp --quiet foobar.txt joe.txt
$()
. And typically it is easy to handle it if the output is gathered into a variable:LS_FILES=$(ls -l)
echo "Something""Some other thing"
echo "$FOOBAR".tmp
echo .tmp"$FOOBAR"
echo "$FOOBAR1""$FOOBAR2"
FOOBAR="$FOOBAR".tmp
0
on success and non-zero value on failure. The exit value of a program invoked in the script can be accessed using the $?
variable.cmp --quiet foobar.txt joe.txt
CMP_EXIT_VALUE=$?
test
command. This prints out 1 because 100 is not equal to 9:FOOBAR=100
test $FOOBAR -eq 9
echo $?
While this command can be used by itself, it is also important for writing if-else expressions. The test command has another equivalent form [...]
that is more commonly used in if-else.
These are equivalent:
test $FOOBAR -eq 9
[ $FOOBAR -eq 9 ]
Note that it is important to put spaces after [
and before ]
to avoid some Bash problems.
There is another [[
operator that is more complicated and is generally preferred to [
. More info on that can be found here.
for FNAME in $(ls -1)
do
echo "File:""$FNAME"
done
It is common practice to roll up the do
to the previous line by using ;
:
for FNAME in $(ls -1); do
echo "File:""$FNAME"
done
while [ 1 -eq 1]; do
echo "Hello"
done
if
, else
and elif
. The conditions are written using the test [
command or the [[
commands (see above).A simple example:
FOOBAR=99
if [ $FOOBAR -eq 100 ]
then
echo "FOOBAR is 100"
elif [ $FOOBAR -eq 99 ]
then
echo "FOOBAR is 99"
else
echo "FOOBAR is neither 100 nor 99"
fi
It is common practice to roll up the then
to the previous line by using ;
:
if [ $FOOBAR -eq 100 ]; then
echo "FOOBAR is 100"
fi
foobar
exists:if ! command -v foobar --blah --blah &> /dev/null
then
echo "Program foobar does not exist"
fi
This can be used for example to compare file extension:
if [ ${FPATH: -3} != ".py" ]
then
echo "It is a Py file"
fi
set -e
set -o pipefail
set -u
foobar() {...}
. Notice there is no return type and no input parameters. You call the function by just calling its name (without parenthesis). If you want to pass arguments, just put them after the function name (no commas to separate them). The parameters can be accessed inside as $1, $2, ...
. A function can only return an integer in the range 0-255
, with non-zero value indicating error.foobar() {
echo $1 # Print first param
return 1 # Return value of function
}
foobar # Call without arguments
foobar "hello" "world" # Call with two arguments