Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$user or $whoami not working in a bash shell script

Tags:

bash

unix

I am learning basic unix shell scripting now. I was trying a code from here to write my username but it is not working.

The code is:

#
# Script to print user information who is currently logged in , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0

I tried $whoami instead of $user, but still it is not showing my username. What can be the issue here? I am using vim editor in Ubuntu.

like image 880
Mistu4u Avatar asked Sep 21 '25 02:09

Mistu4u


1 Answers

  1. If $USER is not working try, $LOGNAME. If you have already learned about command substitution then you can use $(whoami) or $(id -n -u). Ref
  2. \c in echo wont work unless you specify with -e (stands for enable interpretation of backslash escapes).

    echo -e "Today is \c ";date
    

    It seems you want to prevent the trailing new line character introduced by echo. Another way to achieve this is to just add -n. Then you don't need -e and \c.

    echo -n "Today is "; date
    
like image 199
Shiplu Mokaddim Avatar answered Sep 22 '25 15:09

Shiplu Mokaddim