Command Substitution

  • Run the command inside backquotes (`...`), replace that command with its output.
  • Example: Print all files modified in past day:
    % lpr `find * -type f -mtime -1 -print`
    How does that work?
    1. Shell runs command in backquotes and collects its output:
      find * -type f -mtime -1 -print
    2. Shell runs "outer" command, substituting previous results:
      % lpr afile bfile subdir/cfile
  • Example: edit all files whose text has the word "CAUTION":
    % vi `grep -l CAUTION *`
  • Alternate syntax in bash: $(...)
    % vi $(grep -l CAUTION *)
  • Alternate syntax makes nesting easier:
    % vi $(grep -l CAUTION $(find * -mtime -1 -print))
< previous  index  next >

Contact Jerry Peek