Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script file like this:

#!/bin/bash
clear 

echo "The script starts now."
echo "Hi, user!"
echo 

echo "I will now fetch you a list of connected users:"
echo 
w

echo "I'm setting two variables now."
COLOUR="black"
VALUE="9"

echo "This is a string: $COLOUR"
echo "And this is a number: $VALUE"
echo 

I observed that the effect of w is that it prints the following:

 18:06:36 up  7:43,  3 users,  load average: 0.40, 0.56, 0.64
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
hes      tty7                      10:23    7:43m 12:31   0.96s gnome-session -
hes      pts/0    :0               15:41    4.00s  1.38s 11.46s gnome-terminal
hes       pts/2    :0               17:20   44:52   0.79s 11.46s gnome-terminal

when I typed echo " statement" in terminal , it prints the statement. But I could not understand w here. in terminal echo w , prints w not as above. I am a beginner in scripting, so can someone help me in understanding how this work?

share|improve this question
    
Please see man w –  andr3w Jan 6 at 12:52
    
Oh, I did not know this one character command!! Thanks for your help. –  user2799508 Jan 6 at 13:03
    
Note: there is no difference between a "string" and a "number" in bash. Even declare -i does not change type, it just artificially adds limitations. –  Chris Down Jan 6 at 13:08
add comment

1 Answer 1

I really don't understand what you're doing. w is a command and your scriptis running it, what were you expecting?

## This echo prints the string within the quotes: "I will ..."
echo "I will now fetch you a list of connected users:"
## This echo prints an empty line
echo 
## This runs the command 'w' which prints connected users
w

echo w on the other hand will simply print the letter w since you are not running the command. If you want to echo the command's output, run echo $(w). That, however, is pointless since it will loose the formatting and simply running w is enough.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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