ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, I am making a script which looks for a field in a text file and assigns it to a variable, for example:
1:23:34:45:45:banner "hola" | write userx pts/x
As you can see, the sixth field corresponds to the command I want to execute. I use the following command for the assingment:
command=`awk -F: '{print $6} file'`
As a result the variable command is correctly assigned the value:
banner "hola" | write userx pts/x
I try to execute it on the script with the following instruction:
$command
But the shell only executes the banner command, wich takes as an argument the rest of the value of command variable, printing a banner with the string " hola | write userx pts/x ", instead of passing the ouput of the banner "hola" command to the command write userx pts/x, to send it to userx. I have tried using "$command", but it doesn´t work either. If the command doesn´t have pipes, it works ok.
I am using Red Hat Linux.
Thanks for the help.
Last edited by DeepSeaNautilus; 08-04-2008 at 07:16 PM.
Shell meta characters are not interpreted inside quotes when executed directly.
This isn't quite correct. Redirection and pipes are done before parameter expansion, but file name expansion is still performed:
Code:
$ touch a b c d
$ ls ?
a b c d
$ set -x
line 21: set -x
$ files='?'
line 22: files='?'
$ $files
line 23: a b c d
bash: a: command not found
As you can see, the shell did not perform pathname expansion inside the single quotes, but it did perform parameter expansion, and then file name expansion (wildcarding) and then command execution.
The important point here is knowing the order of command line expansion.
But the order is not relevent for the OPs problem. I see your point that wildcards are not expanded upon assignment, only when the parameter is dereferenced, but that does not affect the pipe character, since it is not related to expansion. In order for the | to be handled properly within quotes, one must use eval.
Quote:
Redirection and pipes are done before parameter expansion
I also don't follow this, all expansion must be done before any pipeline/redirection.
You said: "Shell meta characters are not interpreted inside quotes when executed directly."
I say: This is not correct - some meta chars ARE interpreted inside (double) quotes when executed directly.
Quote:
Originally Posted by Mr. C.
Redirection and pipes are done before parameter expansion
Quote:
Originally Posted by smoked kipper
I also don't follow this, all expansion must be done before any pipeline/redirection.
No, I'm saying the shell parses a command line for pipes and redirection symbols *before* any other type of expansion. The command line is then expanded next. This is why pipe and redirection is not subject to parameter expansion, while file name expansion is.
You said: "Shell meta characters are not interpreted inside quotes when executed directly."
I say: This is not correct - some meta chars ARE interpreted inside (double) quotes when executed directly.
Sorry, I was a bit sleep deprived the other day (I should probably refrain from posting when I haven't slept for 24+ hours), I think I went off on a bit of a tangent. I'd forgotten that most shells do filename globbing in parameter expansion (as mentioned, zsh doesn't do this by default, though there's an option to enable it).
Quote:
No, I'm saying the shell parses a command line for pipes and redirection symbols *before* any other type of expansion. The command line is then expanded next. This is why pipe and redirection is not subject to parameter expansion, while file name expansion is.
Ah, parsing, I'm with you now (I was a bit confused when you said 'order of expansion', since pipes/redirections are not related to expansion). You mean the shell parses the command line and builds the syntax tree, registering any redirections along the way, then it traverses the syntax tree, performing any expansion/substitution. Gotcha.
Thus, e.g.
Code:
cmd='echo * > file'
parses as a single "word", hence no redirection is indicated in the syntax tree. And since the shell performs filename matching in parameter expansion, it expands the '*' when you dereference the variable, but the '>' is not treated specially at the execution stage.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.