Useful Bash Knowledge to Know
TL;DR
A collection of useful bash commands.
Basics
Outputting long text
Storing command results in a variable
Wrap with $() or backticks.
Storing standard input in a variable instead of reading from a file
Arithmetic
Integer arithmetic
Division truncates toward zero.
Floating-point arithmetic
Use bc.
Getting files
Manipulating file names
Removing the extension
Using % is readable and convenient. It removes the matching string from the end. This can be applied to more than just extensions.
Extracting the file name and directory from a path
Using basename is easy. Another approach uses ##, which removes the longest match from the beginning.
Extracting information from a file name using delimiters
%% removes the longest match from the end.
Batch renaming file extensions
.txt -> .csv
Reading CSV or TSV files with basic formatting
The -s option of column defaults to tab, so for TSV files you can omit -s.
Converting delimiters in CSV and similar files
csv -> tsv
Checking whether a variable is defined
You can check with [ -v variable ].
Using aliases in shell scripts
In non-interactive mode, alias does not work by default.
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).
This enables aliases. Since functions can achieve the same thing, using functions might be easier.
An example of running Docker while mounting the current directory.