[PLUG] conditional redirect in shell script

Paul Heinlein heinlein at attbi.com
Wed Jun 5 19:39:36 UTC 2002


On Wed, 5 Jun 2002, Galen Seitz wrote:

> I want to conditionally redirect stdout to /dev/null in a shell
> script. However, it appears that redirection occurs before variable
> expansion, which means my approach won't work.  What is the correct
> way to do this?

You've got to eval it. Take a small example script:

#!/bin/bash
redir='> tmp.txt'
echo 'hello' $redir

Running that will produce unwanted output:

  hello > tmp.txt

Adding an eval statement makes things right:

#!/bin/bash
redir='> tmp.txt'
eval echo 'hello' $redir

--Paul Heinlein <heinlein at attbi.com>






More information about the PLUG mailing list