What's the problem?

I ran the following script:

#!/bin/bash
DATE = date
echo “Today’s date is $DATE.”
USER = who | wc -l
echo “There are $USER users currently logged in.”

Whilst the first echo command displayed the expected output, the second displayed the following: ./demo.sh: line 7: 1: command not found.

As a mere novice I am totally confused!!! Could someone please advise???

1 Like

I see spaces where there should not be, and I see missing brackets and $ signs.
To assign a value to a variable in shell script from an output of a command, you need to do
variable=$(command)

That’s why

DATE=$(date)
USER=$(who | wc -l)

Would make much more sense, I think.

Oh, almost forgot it: welcome to the forum!!! :smiley:

2 Likes

Excellent…that worked a treat!!! Thank you for your help and kind reply…and for the welcome to the forum :grinning:

1 Like

Using backticks for executing anything is deprecated since forever.

2 Likes

how do you list the users logged in? with “who”?

I appreciate that Akito…I was truly baffled because I was following an exercise from a “reputable” training provider.

I simply type “who” and hit enter. Alternatively, I believe you can also type “w” which will give some added detail.

Hope this helps.

1 Like

These are the main reasons I see those backticks all over the place:

  1. The articles showing them are at least 10 years old.
  2. Whoever wrote that article is very old and is shell scripting for at least 20 years, but never changed or improved or adjusted their skills in the past 10 years.
  3. People who “do not care” about deprecated stuff, because “it still works”.
1 Like

thanks for the info. i tried w and who. they both work.

1 Like

In the OP I didn’t see backticks. Did I miss something?

Pretty sure that’s discourse reading those backtick characters as markdown/markup…

Forgot to put them :face_with_head_bandage:
But I had them on the original script I tried to run.

Try using whoami command in simple
Why to use complex commands ??

1 Like