1

This is my alias configuration in Ubuntu 16.04.2 LTS

root@Ubuntu:/# cat /etc/.bash_aliases 
alias i='ifconfig | grep eth -A 1'
alias l='ls -lh'
root@Ubuntu:/# 

root@Ubuntu:/# cat /etc/bash.bashrc
### *output truncated* ###
if [ -f /etc/.bash_aliases ]; then
    . /etc/.bash_aliases
fi
root@Ubuntu:/# 

However, when I test it, only alias i='ifconfig | grep eth -A 1' is working as expected.

root@Ubuntu:/# i
eth0      Link encap:Ethernet  HWaddr AA:AA:AA:AA:AA:AA  
          inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0
root@Ubuntu:/# 

root@Ubuntu:/# alias i
alias i='ifconfig | grep eth -A 1'
root@Ubuntu:/# 

Another alias alias l='ls -lh' does not work as expected.

root@Ubuntu:/# l
bin/   dev/  home/
root@Ubuntu:/# 

The output should be like this:

root@Ubuntu:/# ls -lh
total 80K
drwxr-xr-x   2 root root 4.0K Jul 21 14:37 bin
drwxr-xr-x   3 root root 4.0K Jul 21 14:52 boot
drwxr-xr-x  15 root root 3.8K Nov  3 12:22 dev
root@Ubuntu:/# 

It turns out that l does not follow my alias config.

root@Ubuntu:/# alias l
alias l='ls -CF'
root@Ubuntu:/# 

I guess alias l='ls -CF' must be configured at somewhere else.

The question is how to find out the location of alias l='ls -CF' config file?

wjandrea
  • 14,504

1 Answers1

2

There already exists alias for l in .bashrc ( in fact, it is defined in /etc/skel/.bashrc and that is copied to user's home directory when user is created) and that's what's overwriting yours:

$ grep '^alias l=' /etc/skel/.bashrc                                                                                                                                   
alias l='ls -CF'
$ grep '^alias l=' ~/.bashrc                                                                                                                                           
alias l='ls -CF'

However, that's not the only force at play here. What also happens is that bash sources files in specific order (and depending on the mode in which it has been started - interactive, non-interactive, remote shell, etc.). The general idea is that it goes from files defined in /etc/ to files defined in your home directory, so your alias indeed does get defined when bash sources /etc/.bash_aliases, but then it gets re-defined when bash sources your ~/.bashrc.

Here's a demo:

$ # define alias in your file and let it source via /etc/bash.bashrc
$ echo "alias sayhi='echo hi'" | sudo tee -a /etc/.bash_aliases
[sudo] password for xieerqi: 
alias sayhi='echo hi'
$ echo "[ -f  /etc/.bash_aliases ] && source /etc/.bash_aliases" |          
> sudo tee -a /etc/bash.bashrc
[sudo] password for xieerqi: 
[ -f  /etc/.bash_aliases ] && source /etc/.bash_aliases
$ # start new interactive shell
$ bash
$ sayhi
hi
$ # Works, right ? now define same in ~/.bashrc
$ echo "alias sayhi='echo nope'" >> ~/.bashrc
$ # and start shell again, let all files source
$ bash
$ sayhi
nope

Note that bash --posix , i.e. when shell is in posix mode, the ~/.bashrc and /etc/bash.bashrc apparently aren't sourced; I tested multiple times, but my alias invocation didn't produce any results in that mode.

$ echo "echo 'I am bashrc'" >> ~/.bashrc
$ bash
I am bashrc
$ exit
$ bash --posix
bash-4.3$