1
#Get client IP base on current logged in user
if [ $USER == 'root' ]
then
    ip="$(last | awk 'NR==1 {print $3}')"
else
    ip="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
fi

/root/.bashrc:157: = not found

Line 157

enter image description here


Note

I appended this line source ~/.bashrc to my .zshrc to

vi .zshrc
source ~/.bashrc  # import all my quick aliases and fns
7
  • 2
    Why are you parsing (or attempting to parse) the .bashrc if you're using zsh? – roaima May 24 at 16:37
  • I am so sorry, I updated my post. The issue now on line 157. I showed what it is. – cyb3rZ May 24 at 16:40
  • I am trying to load make my .bashrc file also compatible with .zshrc. – cyb3rZ May 24 at 16:41
  • Please don't post images of text. Instead, copy/paste the text into your question and use the formatting tools to format it as code. That way, we can actually copy the code to test it and we don't need to manually type it out. – terdon May 24 at 17:04
  • 1
    Only mean to show line number as Proof. Code is there also. – cyb3rZ May 24 at 17:14
4

In zsh, =cmd is a filename expansion operator that expands to the path of the cmd command. =cmd is similar to $commands[cmd].

So here, with == in one of the arguments of the [ command, that expands it to the path of the = command. As there's no command called = in your $PATH, that causes an error.

Compare:

$ echo =ls
/bin/ls
$ echo =junk
zsh: junk not found

The equality operator in the [ command is =. The [ command only does tests, it doesn't do any assignments, so there's no need to differentiate between an assignment operator and an equality comparison operator like there is in some other languages (with = vs == like in C or := vs = in some others, etc).

So it should just be:

[ "$USER" = root ]

Still the [ of zsh, like that of ksh also supports == as an alternative to =, but unless you disable the equals option (like it is in ksh emulation), you'd need to quote at least the first = to avoid that =cmd operator:

[ "$USER" '==' root ]

Note that while $USERNAME is automatically set by zsh, $USER is not (though it is set as an environ variable by some things like login).

To test whether you have superuser privileges, it's more robust to check that your effective user id is 0, which can be done in zsh or bash with [ "$EUID" -eq 0 ] or (( EUID == 0 )).

See also:

3

This line is not really syntactically correct (but bash handles it):

if [ $USER == 'root' ]

You should use == only with [[...]], and = otherwise. I'd also recommend you double-quote $USER if you're staying with [...] syntax

if [[ "$USER" == 'root' ]]

or

if [ "$USER" = 'root' ]
2

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.