0

I am using echo var >> "${log_file}" 2>&1 to redirect stdout and stderr to a file path stored in the ${log_file}. But when the variable is empty, I would like to send stdout and stderr to their default/standard locations.

For example, if the variable ${log_file} is empty, the output should not go to a file, but to stdout. How I should set the ${log_file} variable value to make it output to stdout?

I tried setting to &1 (the stdout file descriptor)

if [[ -z "${log_file}" ]]; then
    log_file=&1
fi

echo var >> "${log_file}" 2>&1

But it did not work and bash threw this error:

No such file or directory
1: command not found
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
  • On a more general approach to logging bash output, you might want to check out [this question](https://stackoverflow.com/questions/3173131). (Doesn't require to redirect each command's output individually.) – DevSolar Nov 26 '19 at 13:00
  • How about using `exec` to create the file descriptor? Can't you redirect in the `if ... else` branch directly? – stephanmg Nov 26 '19 at 13:02
  • `>>&1` is a redirection operator that has to be parsed *before* parameter expansion happens. There isn't really a clean, short way to do what you want. – chepner Nov 26 '19 at 13:44

1 Answers1

1

You have two fundamentally different operations based on the value of the variable:

  1. Redirection both stderr and stdout to the named file
  2. Don't redirect either.

There's no syntax to handle this dichotomy. I would recommend the following:

  1. Wrap the command whose output you might want to redirection in a function.

    func () {
        echo var
    }
    
  2. Use an if statement to call that function with and without redirection, as necessary.

    if [ -n "$log_file" ]; then
        func >> "$log_file" 2>&1
    else
        func
    fi
    

Another approach is to define func conditionally, attaching the redirection directly to the function.

if [ -n "$log_file" ]; then
    func () {
        echo var
    } >> "$log_file" 2>&1
else
    func () { 
        echo var
    }
fi

func
chepner
  • 497,756
  • 71
  • 530
  • 681