Bash – List only temporary aliases in bash

aliasbash

I know that to list all aliases in a given bash session with alias -p. Is there a way to get a list of all the temporary aliases in a given bash session, i.e. all aliases that aren't in my bash profile?

Thanks!

Best Answer

It's not that simple. There is no concept of "temporary" aliases in bash, and for bash any command executed by sourcing .bashrc is the same as any you type into the command line. Moreover the bash profile files could define some aliases only under some circumstances.

You could save into a variable (or file) the aliases which are set after the bash profile files are executed and then, when you need it, check the difference between such variable and the aliases currently set:

$ BASE_ALIAS="$(alias | sort)"
$ alias tmp_alias=""
$ unalias ls
$ diff <( echo "$BASE_ALIAS" ) <( alias | sort )
3d2
< alias ls='ls --color=auto'
5a5
> alias tmp_alias=''

diff shows that an alias ls has been removed and an alias tmp_alias has been added since the declaration of BASE_ALIAS.

Related Question