0

So, in the following example:

echo "text to write to file but also not send to stdout" | tee -a $logfile 1> /dev/null

The output does not get printed on the terminal. But when I try doing the same action based on a variable it does not work.

Ex:

$loud=""
if [ -z $loud ]; then
  route_devnull="1> /dev/null"
else
  route_devnull=""
fi
echo "text to write to file but also not send to stdout" | tee -a $logfile $route_devnull

How can I get the second example to work? Or should I try another way to suppress the messages?

  • You should explain why you are trying to do this. – jeremysprofile Mar 28 '19 at 3:57
  • Well, the code kind of gives it away but it's meant to suppress the printing of messages that also get written into a log file. Basically to allow printing of extra debug info on the terminal rather than reading it from a log file later. – anonymite Mar 28 '19 at 5:21
1

TL;DR: echo "text to write to file but also not send to stdout" | eval tee -a $logfile $route_devnull

bash does not evaluate variables as commands. You can write

echo "abc && touch new"

and it will print abc && touch new to the terminal. This is by design, since strings can be input from users, and we don't want to trust their commands, since they can do malicious things like trying to break our computer.

This is where eval comes in.

Instead of first executing the command, eval reads and evaluates all the arguments to construct another command and then execute it. Example:

eval echo "abc && touch new"

Just shove eval before whatever you want evaluated before execution. And by "just shove" I mean "think very carefully about using eval"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.