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,236
  • 4
  • 48
  • 98
  • 1
    Read https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files -- start with /etc/profile, look at all the files it sources. Then look at your own .bash_profile or .profile and look at all the files they source. Then look at your .bashrc and all the files it sources. – glenn jackman Nov 03 '17 at 14:44
  • 1
    I'd suggest you put your own personal preferences in your ~/.bashrc and leave the /etc/... files alone. – glenn jackman Nov 03 '17 at 14:45
  • 2
    … and don't work with the root account, but rather with a normal user. – dessert Nov 03 '17 at 14:49
  • 1
    IIRC alias l='ls -CF' is defined in /etc/skel/.bashrc, which by default will get copied to all new accounts on creation (including that of root) – steeldriver Nov 03 '17 at 14:49
  • Thanks @glennjackman for your feedback.

    The reason why I put this on /etc/... and not my own directory because this is a test image and I've multiple test users.

    I would like to use the same alias on each test users.

    Is there a way to find this alias file automatically instead of manual way?

    – Charlotte Russell Nov 03 '17 at 14:51
  • Not that I'm aware of. – glenn jackman Nov 03 '17 at 15:11
  • 2
    Well, you can't get around the fact that the last (re)definition of an alias will always apply; since users' preferences always take precedence your options are either to modify the default /etc/skel/.bashrc or (since it sources ~/.bash_aliases if present after the default alias definitions) provide a suitable ~/.bash_aliases that overrides it again. – steeldriver Nov 03 '17 at 15:18
  • 1
    Another option is you could just change the name of the alias to something like lh, so it won't get overwritten by the default ~/.bashrc. This has an added benefit of not confusing users who expect l to be aliased to ls -CF. – wjandrea Nov 04 '17 at 01:36

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$ 
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497