A capability built into *n?x shells (as well as Perl) for taking the output of one program and turning it into command-line arguments (*not* input) of another program.

Command substitution is invoked by surrounding a shell pipeline with grave accent (aka backtick) characters. The shell takes whatever is written to stdout from that pipeline and turns it into a command-line argument for the program the grave-accent-enclosed pipelien appeared on.

To wit:

mv $f `dirname $f`/`basename $f|tr '[a-z]' '[A-Z]'`

Suppose the value of f is /etc/passwd.

The shell will expand variables first, resulting in

mv /etc/passwd `dirname /etc/passwd`/`basename /etc/passwd|tr [a-z][A-Z]`

Now, if you typed

$ dirname /etc/passwd

on the command line, the dirname program would print


and if you typed

$ basename /etc/passwd|tr '[a-z]' '[A-Z]'

on the command line, you would get


as output.

Since those two commands are in grave accents, our original command is finally invoked as

mv /etc/passwd /etc/PASSWD

resulting in a fun time for all your users. As an exercise, try to work out:

tar cvf mm.tar *.mif `ls *.mif|sed 's/mif$/mid/'`

Baffo was nosy enough to point out that command substitution works the same way in Perl; Frater 219 pointed out that the grave accent has a new name now. I don't know if I can put up with this kind of harassment.

Log in or register to write something here or to contact authors.