Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Both don't use correct syntax, although bash does know how to interpret the first example.
X="ls"; $X (X=ls, then execute $X, which will execute ls)
should be: X="`ls`" ; echo $X (X is filled with the output of ls, then $X is echoed.
X="ls | wc"; $X (X= ls | wc, $X is executed. ls tries to list |, and wc is not understood.
should be: X="`ls | wc`" ; echo $X (X is filled with the result of ls | wc, then $X is echoed)
The reason it's working differently, as far as I can tell, is because you are doing two different things here, or doing basically the same thing but in two different ways.
In the first example: X="ls -al"; $X
X is equal to the string "ls -al"
using the command $X is just telling bash to replace $X with "ls -a" and it runs the string as a command.
In the second example: X="`ls -al | grep root`"; echo $X
X is equal to the output of "ls -al" (notice the ` which tells bash to run its contents as a command)
In this case, $X is not the command, but rather an argument to "echo"
In addition, the first example puts out ls -al and stops there. The second example pipes the output to grep, which outputs only lines containing "root"
X="ls -al" ; $X - $X will execute a 'normal' ls -al. 'Normal' being the way you are used to when entering the ls -al command on the command line, all output lines by itself.
X="`ls -al`" - $X is one string. The echo command will display the string as is (one line). If you want to break up this line you could use printf (see man 3 printf or a book: unix/linux in a nutshell or a good book about C). But this might not work for you. You need to tailor printf for specific output (ls looks different then say ls -latr).
The following sollution works, but I only use it when absolutely neccesary:
I know this is an old thread, but I got this problem now
thank you (me too) for the answer is working ...
just comment about mechanics:
to "see" why is not working when you don't use eval or bash -c (this asume that you can write in current directory ...)
PHP Code:
echo "z='ls -1 | wc -l' ; "'$z' > z ; bash -x z + z='ls -1 | wc -l' + ls -1 '|' wc -l ls: cannot access |: No such file or directory ls: cannot access wc: No such file or directory
as you can see (on 3th line), when string is executed the pipe character will be surrounded by ' characters and that remove it special (pipe) signification
(long live bash for doing that ...)
another solution (less elegant, but who knows, someone may find it better) is:
z='ls -1 | wc -l' ; bash -c "$z"
Last edited by sorcerer25; 12-09-2015 at 09:59 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.