I created a bash script to thumbnail all images in a tree. It is the following:
#!/bin/bash
find -path "thumbnails/" -prune -or -iname "*.jpg" -exec \
bash -c 'convert "$0" -resize 256x256 thumbnails/`sha512sum "$0" | awk "{ print \\$1 }"`.jpg' {} \;
# ^^
In the awk command, there is a double \\. (I’ve marked it with ^^ on the commented line, but you’ll probably need to scroll →) Why do I need two backslashes here? I need one to prevent the shell from attempting to expand $1, but otherwise, we are working within just a single set of single-quotes, which shouldn’t be messing with the number of slashes. Yet, with just one backslash, awk { print } gets executed, which isn’t correct.
Why the \\?
There are actually two shells here which do all the usual variable/path substitions/expansions, one is
the other is the backtick operator:
You need another backlash to prevent expanding of
$1.