Bash Commands
GNU Bourne-Again Shell and its scripting language.All Bash pages
- bash check if a file exists ([ -f file ], [ -e ], [ -d ])Check if a file exists in bash with [ -f "$file" ], a directory with -d, any path with -e, and negate with !. All test operators and the quoting pitfalls.
- bash check if a string contains a substring ([[ $str == *sub* ]])Check if a string contains a substring in bash with [[ $str == *"$sub"* ]], a POSIX case pattern, regex with =~, or grep -q. Quoting and case-insensitive tests.
- bash loop through files in a directory (for f in *.txt)Loop through files in a directory in bash with for f in dir/*.txt. Quote "$f", use nullglob for no matches, globstar or find to recurse, and avoid $(ls).
- bash read a file line by line (while IFS= read -r line)Read a file line by line in bash with while IFS= read -r line; do ...; done < file. Why IFS= and -r matter, keep the last line, mapfile, and stdin traps.