aboutsummaryrefslogtreecommitdiffstats
execline: the backtick command

execline
Software
skarnet.org

The backtick program

backtick runs a program and stores its output in an environment variable, then executes another program.

Interface

In an execlineb script:

     backtick [ -i | -I | -x | -D default ] [ -N | -n ] [ -E | -e ] variable { prog1... } prog2...
  • backtick reads prog1... in a block and unquotes it.
  • It runs prog1... as a child process and saves its output in memory. This output must not contain a null character.
  • backtick execs into prog2..., with variable added to the environment with prog1...'s output as a value.

Options

  • -N : store prog1...'s output as is, including the last newline, if any.
  • -n : chomp an ending newline off prog1...'s output. This is the default.
  • -e : no autoimport. This is the default.
  • -E : autoimport. Before exec'ing into prog2..., perform a substitution so that $? is replaced with prog1...'s exitcode (if applicable, see below), and ${variable} is replaced with prog1...'s output, instead of keeping the results in the environment. The variables will not appear in the environment; if they preexisted in backtick's environment, they will be removed.

The other options tell backtick what to do if prog1...'s output is not suitable as the contents of an environment variable (i.e. it contains a null character) or if prog1... crashes or exits nonzero:

  • -i : backtick exits with an approximation of prog1's exit code, or 124 if prog1 wrote a null character. This is the default.
  • -I: the value of variable is set to whatever the start of prog1...'s output is, up to the first null character, or to whatever prog1... wrote before crashing; chomping is applied if required; then execution proceeds.
  • -x : variable is removed from the environment, and execution proceeds.
  • -D default : the value of variable is set to default, and execution proceeds.

If the command line can keep running after prog1 crashes or exits nonzero, i.e. if one of the -I, -x -or -D has been given, then backtick also adds the ? variable to the environment. If prog1 exited normally, the variable contains its exit code; otherwise, 256 plus the number of the signal that killed it.