Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

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.

share|improve this answer

You could do this with the following:

comm -2 -3 --nocheck-order <(alias -p) <(bash -l -c 'alias -p')

This works by comparing the current list of aliases with the aliases present in a brand new shell.
The comm utility is used to show only lines that are present in the first command <(alias -p). The --nocheck-order is necessary to keep it from complaining that the alias list isnt in order, even though it really is...

share|improve this answer
    
The aliases defined by the bash profile files for a brand new shell could be different from the ones defined by those same files for the current shell (i.e. [[ $(( $RANDOM % 2 )) == 0 ]] && alias ...). Nice hint about using comm though! –  peoro Apr 18 '12 at 1:27
    
@peoro nobody is going to do that though. Seriously, you start a shell from the exact same TTY as the original shell, the environment is the same, there is no reason why aliases would be different. –  Patrick Apr 18 '12 at 13:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.