I do not understand why the output is the username because in line 3 and 4 must print /usr/bin/whoami
.
please explantation simple to me
#!/bin/bash
WHEREWHOAMI="`which whoami`"
ROOTORNOT="`$WHEREWHOAMI`"
echo "$ROOTORNOT"
I do not understand why the output is the username because in line 3 and 4 must print /usr/bin/whoami
.
please explantation simple to me
#!/bin/bash
WHEREWHOAMI="`which whoami`"
ROOTORNOT="`$WHEREWHOAMI`"
echo "$ROOTORNOT"
The variable ROOTORNOT
is set to the output of the execution of WHEREWHOAMI
which in turn is the output of the command which whoami
.
WHEREWHOAMI=`which whoami` # <- /usr/bin/whoami
ROOTWHOAMI="`$WHEREWHOAMI`" # <- `/usr/bin/whoami` # <- username
You can easily figure out what is going on if you add the set -x
flag to your script. Example:
$ set -x
$ WHEREWHOAMI="`which whoami`"
++ alias
++ declare -f
++ /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot whoami
+ WHEREWHOAMI=/usr/bin/whoami
$ ROOTORNOT="`$WHEREWHOAMI`"
++ /usr/bin/whoami
+ ROOTORNOT=kvantour
$ echo "$ROOTORNOT"
+ echo kvantour
kvantour
$
Backticks are evaluated even inside double quotes.
(Suggestion - don't use backticks. use $()
instead.)
WHEREWHOAMI="`which whoami`"
This executes which whoami
and assigns /usr/bin/whoami
to WHEREWHOAMI
.
ROOTORNOT="`$WHEREWHOAMI`"
This executes /usr/bin/whoami
in backticks, and assigns the USERNAME result to ROOTORNOT
.
It's doing exactly what it should.
Is that not what you indended?
Perhaps what you wanted was something like -
$: [[ $( $(which whoami) ) == root ]] && echo ROOT || echo not-root
not-root
Though I do suggest storing the value and comparing that. Is there a reason you can't just use
if [[ root == "$LOGNAME" ]]
then : ...
?