BASH command substitution that starts with a pipe |
I'm having trouble substituting a command that starts with a pipe |
Typing the command directly works fine: echo "some output" 2>&1 | tee -a afile But instead of having to add 2>&1 | tee -a afile to each and every echo statement I want to substitute it for a variable, $TEE, using it like this: echo "some output" $TEE I have tried quite a couple of ways but can't seem to get it to work, TEE=2>&1 | tee -a afile <- probably doesn't even execute the command at all when I put $TEE after the echo command as described above. Using eval and TEE="2>&1 | tee -a afile" <- doesn't seem to care about the preceding echo part echo "..." eval $TEE MYVAR=`cmd` executes the command and stores the output in the variable, = not what I'm looking for. TEE=`2>&1 | tee -a afile` <- executes the command at the wrong time?, and therefore probably doesn't include the echo "some output", thereby making the command execution useless. Any ideas? /Kristofer |
This should work:
Code:
function echo() { [edit:]It's obviously better to use "/bin/echo" instead of "echo" in the echo function... You'll have to use the exact path though.[/edit] |
You should quote the TEE assignment:
Code:
TEE="2>&1 | tee -a afile" Code:
$ echo 'J. R. Bob Dobbs' $TEE Code:
J. R. Bob Dobbs 2>&1 | tee -a afile Code:
eval "echo 'J. R. Bob Dobbs' $TEE" |
Thx
Big thx to booth of you guys. After having tried booth solutions I decided to go for the one using the function since it clutters the original code much less and thereby also making it easier to switch between the echo writing to file and screen and the original writing only to screen.
/Kristofer |
Quote:
Code:
eval "echo QWERTY $TEE" |
All times are GMT -5. The time now is 02:15 AM. |