"Bash is the GNU Project's shell—the Bourne Again SHell."
- Using a variable in a string
- Capturing the output of a command in a variable
- Script name, directory, present working directory
- If/else, comparing strings
- If/else, checking if a directory exists, making a directory
- If/then, checking if a command or binary exists
- Checking if a file exists
- Checking if a variable has zero length
- Redirecting output
- Let the entire script fail if any of it fails
Using a variable in a string
There can not be any space between the variable name, the equals sign and the value.
MY_VAR="test"
echo "The contents of MY_VAR is $MY_VAR"
Capturing the output of a command in a variable
URL=$(python3 get-latest-release-url.py);
echo "Latest release URL is $URL"
Script name, directory, present working directory
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/bash echo "Script name:" echo "$BASH_SOURCE" echo "$0" echo "Directory that contains the currently running script, relative to PWD:" echo "$(dirname $BASH_SOURCE)" echo "$(dirname "$0")" echo "${0%/*}" echo "Present working directory: $PWD" |
Includes the ./
when we call the script like ./my_script
, but not when calling as sh my_script
.
See this stackoverflow post. See also this stackoverflow post.
If/else, comparing strings
String comparison can be done using == and !=, as in most programming languages.
if [[ "$TITLES_ORIGINAL" != "$TITLES_NEW" ]]; then
// ...
fi
If/else, checking if a directory exists, making a directory
if [ ! -d "$DL_DIRECTORY" ]; then
mkdir "$DL_DIRECTORY";
else
rm $FILE_PATH 2> /dev/null
fi
If/then, checking if a command or binary exists
In this example we check if we can use meld, a file comparison tool.
if command -v meld &> /dev/null
then
meld $ORIGINAL_FILE_PATH $NEW_FILE_PATH
fi
Checking if a file exists
Here we use -f for indicating a file, instead of -d for directory or -v for executables.
if [ -f $FILE_PATH ]; then
# File does exist
fi
Checking if a variable has zero length
if [ -z "$ARG_OUTPUT_PATH" ];
then printf "Output path argument ARG_OUTPUT_PATH has not been set to a valid value"; exit 1;
fi
Redirecting output
We can discard output by redirecting it to the null device.
Standard output is redirected with 1>
.
Error-related output is redirected with 2>
.
Here we want to remove a file, but we don't want to see the error if the file does not exist. By redirecting error output to /dev/null
we throw that output away. dev/null
can be described as a void, a file device that doesn't actually store anything.
rm $FILE_PATH 2> /dev/null
We can redirect both by using &>
.
ls -al 1> /dev/null
my_script.sh &> /dev/null
Let the entire script fail if any of it fails
To let any failure make the entire script fail, we can add set -euo pipefail
at the top.
Like so:
#~/bin/bash
set -euo pipefail
echo "The working directory is $PWD";
cd "~/non-existing-path";
echo "Imagine we are removing every file from the directory $PWD";
In this example the script would try change the working directory and remove every file in that directory.
Because we are making the script fail due to the non-existing path, the last line will not be executed.
Which is good, because the cd
failed we would be removing files from a directory that we did not intend to.