sudo
doesn't even support the full "simple command" syntax. According to the bash man page, in the Shell Grammar section:
A simple command is a sequence of optional variable assignments
followed by blank-separated words and redirections, and terminated by
a control operator.
That is, a simple command might be something like this:
LC_ALL=C grep -i "keyword" <infile >outfile &
sudo
supports variable assignments (like LC_ALL=C
) and a command name and its arguments (like grep -i "keyword"
), but does not support redirections (like <infile >outfile
) or control operators (&
). In fact, if you tried to use the unsupported elements with sudo
, like this:
sudo LC_ALL=C grep -i "keyword" <infile >outfile &
... then the shell will interpret the redirects and backgrounding (&
) before running sudo
, and apply them to sudo
itself (not to the grep
command, except indirectly).
Other things sudo
doesn't support: any sort of complex command, including pipelines, lists (multiple commands separated by ;
or &
), command groups (with either ( )
or { }
), conditional and arithmetic expressions ([[ ]]
and (( ))
), logical operators (!
, &&
, and ||
), keywords (if
, for
, while
, case
, etc), or shell aliases and functions.
time
is an interesting case, because it's a shell keyword and also a regular command (generally /usr/bin/time). When you use sudo time somecommand
, it doesn't recognize time
as a shell keyword so it uses the regular command executable version instead.
If you want to use the shell (keyword) version, you can run a shell under sudo
and let it recognize the keyword; something like this:
sudo bash -c 'time echo hello'
Since that runs a full shell, you can also use any other complex shell syntax you want in this form. But be careful about quoting and such; whatever's after sudo bash -c
will be run through the shell parsing (and quote and escape interpretation) twice, so it's easy to get unexpected results.
Sudo
is a regular program which is invoked by the shell as any other program, so your attempt atsudo (...)
is just a syntax mistake. You could trysudo "(...)"
then the command enclosed in double quotes would be passed to the shell in the context of thesudo
'ed user – Serge May 16 '18 at 01:32sudo time echo hello
you are executing the externaltime
command (e.g./usr/bin/time
) rather than your shell'stime
– steeldriver May 16 '18 at 01:37time time
, are bothtime
keyword? https://unix.stackexchange.com/questions/444071/is-each-time-in-these-examples-a-keyword-or-usr-bin-time – Tim May 16 '18 at 03:42