0

I am using bash and I am trying to print a message to standard error if a variable is not set with the following command:

echo ${var:?"This var is not set"}

Now, I want to redirect that error message to a file. I tried the following but it didn't work:

echo ${var:?"This var is not set"} > testfile

Nor the following worked:

echo ${var:?"This var is not set"} 2> testfile

So, how can I direct this generated message to a file?

1 Answer 1

0

From Bash Reference Manual:

${parameter:?word}

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

It may not be obvious but "standard error" here means the standard error of the shell. When you do echo … 2> testfile you redirect standard error of echo. They both normally end up in your terminal but are not the same.

To make it work as you want create a subshell and redirect its standard error:

(echo ${var:?"This var is not set"}) 2> testfile

This will also work:

{ echo ${var:?"This var is not set"}; } 2> testfile

Note the actual command (echo) will inherit the already redirected standard error of the subshell, so effectively this redirection affects them both. It hardly ever matters when the command is echo but with a command that returns error message it does. Compare:

unset var
(dd ${var:?"This var is not set"}) 2> testfile
cat testfile
var=foo
(dd ${var:?"This var is not set"}) 2> testfile
cat testfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .