Bash Scripting
From Leo's Notes
This page was last edited on 31 March 2015, at 18:09.
Extracting / Parsing Filenames
Use the basename to extract filenames from a path and and dirname to extract the path of a filename.
Eg:
dirname /etc/hosts /etc
basename /etc/hosts hosts
To extract only the extension or the filename without the extension, use bash's string matching:
Filename=$(basename /etc/resolv.conf)
Ext="${Filename##*.}"
Name="${Filename%.*}"
Arithmetic Operations
Do integer arithmetic operations on variables within double brackets. For example:
Value=1 ValuePlusOne=$((Value + 1)) ValueTimes12=$((Value * 12))
The operators that are supported are:
+ - / * %
Bitwise operators also work:
^ (XOR) | (OR) & (AND)