2
votes

I am testing the bash behavior on login (terminal 1), but I got confused about its interaction with alias:

I opened with vim .bashrc and add this line :

alias ls='ls -l'

and save it with :x then I used source .bashrc to simulate a new login session and I found it in the aliases list

But I removed the alias from .bashrc and use source .bashrc again I saw that alias ls='ls -l' was still available. On the other hand, opening new shell terminal (terminal 2) the problem was solved.

Question: Why alias ls='ls -l' was not removed on the first terminal ?

1
source does not "simulate a new login" it just executes the operations in the file in the current shell. None of those operations removed the alias. - stark
I mean instead of login again I used source. But the rest of your sentence is not understood, could you please explain more ? - user2971637
source .bashrc is basically the same as copy-pasting the contents .bashrc into your shell. If you copy-paste echo Hello; alias ls='ls -l' then you would expect Hello to be written and an alias to be defined. If you afterwards copy-paste echo Hello, you would NOT expect the ls alias to disappear. Same thing here. - that other guy
Thank you so so much, now it is clear :) - user2971637
Please note the comment in the bash man page, which I believe predates 1996: "For almost every purpose, aliases are superseded by shell functions." IMO, "almost every purpose" is an understatement; there is no need to use aliases. - William Pursell

1 Answers

0
votes

Sourcing .bashrc doesn't clear what you have defined so far. It just adds the definitions it contains to your current environment.

If you want to undefine a given alias, just type:

$ unalias ls
$ source .bashrc

If you want to undefine all aliases:

$ unalias -a
$ source .bashrc

Finally, if you want to start over with a brand new shell, you can of course close your session and reopen one, but here is an almost identical command in case this is not that easy (ssh) or undesirable:

$ exec bash

(you may also add the -l option to simulate a login shell, thus reading your ~/.bash_profile file)