Looking at ways to pass current user's aliases to a sudo
command, I found the following on ArchWiki:
Passing aliases
If you use a lot of aliases, you might have noticed that they do not carry over to the root account when using sudo. However, there is an easy way to make them work. Simply add the following to your
~/.bashrc
or/etc/bash.bashrc
:
alias sudo='sudo '
I don't get why this works. If the shell does not care how many spaces are between two commands, how can this have any effect?
When manually adding a space, I see no difference:
$ alias e=echo
$ sudo e foo
sudo: e: command not found
$ sudo e foo # Manual space addition
sudo: e: command not found # Fail
$ alias sudo="sudo " # Now with alias
$ sudo e foo
foo # Succeeds, but why?
Visibly aliasing sudo to sudo + space somehow allows passing aliases. This works on zsh
, bash
and sh
, so it is not a shell-specific behavior.