Useful Bash commands

2022-02-15 (last modified 2022-03-02) | Martin Hoppenheit | 1 min read

A collection of random Bash commands/idioms/patterns I keep forgetting.

Loops

Do something for a bunch of files, or for all elements of a set or a range:

$ for f in *.jpg; do echo "$f"; cat "$f"; done
$ for i in {1,2,3}; do echo $i; done
$ for i in {1..3}; do echo $i; done

Loop over all lines in a file (the IFS= and -r bits preserve leading whitepace and backslashes):

$ while IFS= read -r l; do echo "$l"; done < a.txt

The same, but simpler and more flexible, with the possibility to use arbitrary Perl code in the argument string (try 'print uc reverse'):

$ perl -nE 'print' a.txt

Parameter substitution

Manipulate a string (like a file path) in a variable. First, a usage example with context:

$ for f in *.tif; do convert "$f" ${f%.tif}.jpg; done

Strip leading (#) or trailing (%) parts of a string:

$x        = foo/bar/b.a.z
${x#*/}   =     bar/b.a.z
${x##*/}  =         b.a.z

$x        = foo/bar/b.a.z
${x%.*}   = foo/bar/b.a
${x%%.*}  = foo/bar/b

Replace substrings according to a pattern:

$x        = foo/bar/b.a.z
${x/b/B}  = foo/Bar/b.a.z
${x//b/B} = foo/Bar/B.a.z

Extract a substring (offset + length):

$x        = foo/bar/b.a.z
${x:4:3}  =     bar