alias - Aliases not available when using sudo - Ask Ubuntu


i playing around aliases today , noticed aliases don't seem available whilst using sudo:

danny@kaon:~$ alias alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -cf' alias la='ls -a' alias ll='ls -alf' alias ls='ls --color=auto'  danny@kaon:~$ ll -d / drwxr-xr-x 23 root root 4096 2011-01-06 20:29 //  danny@kaon:~$ sudo -i root@kaon:~# ll -d / drwxr-xr-x 23 root root 4096 2011-01-06 20:29 // root@kaon:~# exit logout  danny@kaon:~$ sudo ll -d / sudo: ll: command not found 

is there reason why cannot use aliases whilst using sudo?

add following line ~/.bashrc:

alias sudo='sudo ' 

from bash manual:

aliases allow string substituted word when used first word of simple command. shell maintains list of aliases may set , unset alias , unalias builtin commands.

the first word of each simple command, if unquoted, checked see if has alias. if so, word replaced text of alias. characters ‘/’, ‘$’, ‘`’, ‘=’ , of shell metacharacters or quoting characters listed above may not appear in alias name. replacement text may contain valid shell input, including shell metacharacters. first word of replacement text tested aliases, word identical alias being expanded not expanded second time. means 1 may alias ls "ls -f", instance, , bash not try recursively expand replacement text. if last character of alias value space or tab character, next command word following alias checked alias expansion.

(emphasis mine).
bash checks first word of command alias, words after not checked. means in command sudo ll, first word (sudo) checked bash alias, ll ignored. can tell bash check next word after alias (i.e sudo) adding space end of alias value.


Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User