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.
Looking at the man page for alias:
A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded.
Source: http://www.linuxcommand.org/lc3_man_pages/aliash.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With