2004-06-19 · in Tech Notes · 138 words

GNU bash is my usual Unix shell, but in practice I mostly use the POSIX sh subset of it, so I'm equally happy with zsh or pdksh. Here are some tips for sh users.

If you're renaming a file with a long filename where the change is small, you can use {} to your advantage:

mv /some/long/name/{oldfile,newfile}
mv /some/long/name/spelt-{u,i}ncorrectly

If you want to do something to every line of a file, you can use a while loop with read, which puts each line into the $REPLY variable:

while read ; do echo $REPLY | sed 's/foo/bar/g' ; done <file

You can also read lines from several files by using multiple file descriptors:

while read a ; do read b <&3 ; echo $a $b ; done <file1 3<file2

Since rmdir only works on empty directories, you can use it to prune only empty subdirectories:

rmdir *