For Programmers: Free Programming Magazines  


Home > Archive > Unix Shell Programming > October 2007 > command substitution and the pipe









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author command substitution and the pipe
garyjefferson123@gmail.com

2007-10-26, 7:11 pm

suppose I have prepared a command* such as:

~$ cmd="echo \"something1 : something2\" | awk '{print \$1, \$2}'"

and now I want to execute it as if it had been typed at the bash
prompt:

~$ $cmd

which produces:

"something1 : something2" | awk '{print $1, $2}'

I can't see how to get backticks or $() to work for me here, either.

Is there some trick to use to get command substitution to evaluation
the pipe and do what I'm looking for here (besides echoing the command
to a file and then execing it inside a bash prompt)?

Thanks,
Gary



*nevermind that there are other ways to accomplish this without the
pipe -- the sufficiently more complicated example that I'm trying to
solve *cannot* be done without the pipe. The question I'm asking here
is not "how do I do this without the pipe?" it is "how do I get bash
command substitution to work with the pipe in the constructed command?"

Barry Margolin

2007-10-26, 10:11 pm

In article <1193440925.649516.220330@o80g2000hse.googlegroups.com>,
garyjefferson123@gmail.com wrote:

> suppose I have prepared a command* such as:
>
> ~$ cmd="echo \"something1 : something2\" | awk '{print \$1, \$2}'"
>
> and now I want to execute it as if it had been typed at the bash
> prompt:
>
> ~$ $cmd
>
> which produces:
>
> "something1 : something2" | awk '{print $1, $2}'
>
> I can't see how to get backticks or $() to work for me here, either.
>
> Is there some trick to use to get command substitution to evaluation
> the pipe and do what I'm looking for here (besides echoing the command
> to a file and then execing it inside a bash prompt)?


eval "$cmd"

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com