Lilás Lilás - 1 year ago 78
Bash Question

bash: exec $var>&1 doesn't work

How can I use variables in the exec command?

If I do the command below, it works:

$ exec 3>&1

But if I execute the following, it gives me an error:

$ var=3; exec $var>&1
bash: exec: 3: not found

I would like to save in a variable so it won't be hard coded when I need to use the descriptor 3 latter in the script.

Answer Source

Instead of making the file descriptor configurable, let bash itself allocate an unused file descriptor for you.

exec {var}>&1

You can then use $var later in other redirections.

printf '%s' "Some message" >&$var
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download