Bash Idioms
Small, unrelated bash one-liners and patterns worth remembering.
Iterate Over a File
while read -r item
do
echo "$item" # use "$item" here
done < "$inFile"Check If a File Is Sorted and Deduplicated
LC_ALL=C sort filename.txt | uniq -dIf this prints nothing, the file is already sorted and has no duplicates.
Random String Generator
Useful for temp filenames.
randomString () {
tr -dc "a-zA-Z0-9" < /dev/urandom | fold -w 15 | head -1
}ANSI Colors for Output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No ColorCheck If a Folder Exists
if [[ -d "/path/to/folder" ]]; then
echo "Folder exists"
fiif [ ! -d "/path/to/folder" ]; then
echo "Folder does not exist"
else
echo "Folder exists"
fi