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:
.bashrc
if you're usingzsh
? – Chris Davies May 24 '21 at 16:37ip="$(last | awk ...
line to get the IP does not give you the current user's IP address. It gives you the IP (or hostname or tmux pid & window, or login day name, etc) of the last user to causewtmp
to be updated, which isn't necessarily the current user. Trywho -m --ips
(GNU who), but note that reliably getting the current login's IP is more complicated than it initially seems, and if you're doing it from a profile script like .zshrc or .bashrc, you have to handle non-network logins (e.g. console/xterm/serial login, tmux or screen window, etc), and non-login shells too. – cas May 25 '21 at 03:07$SSH_CONNECTION
is fairly reliable if you only allow network logins via ssh. Use that first, and only trywho -m --ips
orlast "$USER"
or eventty
if it's empty. – cas May 25 '21 at 03:13