sudo executes its argument using
exec, not via a shell interpreter. Therefore, it is limited to actual binary programs and cannot use shell functions, aliases, or builtins (
if is a builtin). Note that the
-s options can be used to execute the given commands in a login or non-login shell, respectively (or just the shell, interactively; note that you'll have to escape the semicolons or quote the command).
$ sudo if [ -n x ]; then echo y; fi
-bash: syntax error near unexpected token `then'
$ sudo if [ -n x ]\; then echo y\; fi
sudo: if: command not found
$ sudo -i if [ -n x ]\; then echo y\; fi
$ sudo -s 'if [ -n x ]; then echo y; fi'
testand/or only the
echo. Not the entire
if sudo test? Yes, that'd be better. I can't use
if, because it sets the exit code otherwise.