For example, I have been trying to say "There are 10 people online at the moment" in my script file.

I can never seem to get the command working without the "people online at the moment" part on the next line.

At the moment, I have

w='who | wc -l' 
echo "There are $w people online at the moment" 

However, I always end up with the output

There are who | wc -l users online at the moment 

How do you get the command working in the middle? I've been trying to look and copy examples, but it doesn't seem to help my command substitution issue.


3 Answers 3


You want the output of

who | wc -l

assigned to w, not that string, which is what you get because of the quotes around it. You should use command substitution $(...):

w=$(who | wc -l)
echo "There are $w people online at the moment"

(you can also use the backquotes, but you cannot easily nest those).

  • In some shells, including Bash, you can nest backquotes. It requires nested escapes, see example at stackoverflow.com/a/2657037/776723 Dec 13, 2014 at 7:57
  • 1
    @Polyergic Thanks. I updated my answer, but I think I will stick with nesting $() myself ;-)
    – Anthon
    Dec 13, 2014 at 9:31

Another solution:

echo There are $(who | wc -l) people online at the moment


you should use backtick to execute command

w=`who | wc -l` echo "There are $w people online at the moment"

  • 15
    See why $(...) is preferred over backticks...
    – jasonwryan
    Dec 12, 2014 at 7:06
  • 7
    Valid point. "The backtick is also easily confused with a single quote." exactly what happened here.
    – Ghanima
    Dec 12, 2014 at 7:17
  • @Ghanima: Indeed! This question is a great example for when we need to demonstrate why backticks should be avoided. :)
    – PM 2Ring
    Dec 12, 2014 at 9:04
  • Good, I've upvoted only this answer because it's the only one which explained exactly where the OP had the mistake. Whether backticks are good or evil are just side notes.
    – Ruslan
    Dec 13, 2014 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.