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 -i
and -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
y
$ sudo -s 'if [ -n x ]; then echo y; fi'
y
sudo
only thetest
and/or only theecho
. Not the entireif
statement.if sudo test
? Yes, that'd be better. I can't usetest
without theif
, because it sets the exit code otherwise.