LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   variable question (https://www.linuxquestions.org/questions/linux-newbie-8/variable-question-422580/)

true_atlantis 03-07-2006 02:54 PM

variable question
 
in the following there is a 0 outputted from 'z=ls | wc -w' and i first thought that z would equal 'ls | wc-w' but was confused about the 0, then i thought that the first command z=ls was being piped to wc -w, but then when echoing z i get nothing. my question is what is happening when 'z=ls | wc -w' is run, and why is there a 0 outputted?




atlantis@linux:~> echo $z

atlantis@linux:~> z=ls | wc -w
0
atlantis@linux:~> echo $z

atlantis@linux:~>

pljvaldez 03-07-2006 02:55 PM

Try using back quotes:
Code:

z=`ls | wc -w`

true_atlantis 03-07-2006 03:01 PM

yeah, i understand what was wrong. but im just trying to figure out whats going on to output the 0

spooon 03-07-2006 03:20 PM

the command "z=ls" assigns "ls" to "z" but doesn't output anything, so when you count the words, it's 0

true_atlantis 03-07-2006 08:25 PM

if it assigns ls to z then why when echoing $z does it not output ls?

muha 03-08-2006 03:48 AM

because this does not assign ls to z:
Code:

z=ls | wc -w
I guess the space breaks up the 'assigning to a variable' since this does not assign anything to z:
Code:

z=word word
whereas these do assign ls to z:
Code:

z=ls
z=ls; wc -w <somefile>



All times are GMT -5. The time now is 02:55 PM.