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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to be able to quickly, on-the-fly add (and list/remove) command aliases so that they persist between shell restarts (as if they were added to .bashrc). Are there any ready utilities/scripts/whatever that can help me with that?

Ideally the uses case would look like:

aliasutil add "gcm" "git checkout master"
aliasutil add "gfp %1" "git push --force %1"
aliasutil list
aliasutil remove "gcm"
share|improve this question
Aliases do not support arguments so your use of %1 is not possible, although in that specific case it would work to simply alias gfp to git push --force. Any arguments after gfp will be appended to the expansion. Shell functions can take arguments though. – camh Sep 6 '12 at 13:46
I knew that aliases don't have arguments, but I have described the ideal case. Didn't know about shell functions though. Thanks! – Fixpoint Sep 6 '12 at 14:27
up vote 5 down vote accepted

alias handles creation and listing of aliases. unalias handles deleting them. if you need it, you can get more details with the bash help built-in - help alias and help unalias or in the bash man page.

To save them, I've been using the following for years:

$ alias savealias='alias > ~/.bash-aliases'
$ savealias

All aliases (including the freshly defined savealias will now be saved in ~/.bash-aliases. This is a plain text file which can be edited like a shell script if required. e.g. mine contains lines like this:

alias acs='apt-cache show'
alias acss='apt-cache search'
alias loadalias='. ~/.bash-aliases '
alias mq='mailq | grep "^[0-9A-Z]"'
alias savealias='alias >~/.bash-aliases'
alias ttystat='echo $(tty) $TERM ${COLUMNS}x$LINES'
alias where='type -all'
alias which='type -path'

and many others.

The saved aliases are loaded at login time by adding the following to ~/.bash_profile, ~/.bashrc, or even /etc/profile

[ -e ~/.bash-aliases ] && . ~/.bash-aliases

i.e. "if a .bash-aliases file exists in my home dir, then source it."

With this method, any changes you make to aliases will be lost on logout unless you run savealias - this includes adding, editing, and deleting aliases.

share|improve this answer

I know of no tools that do this, however you should be able to do most of it with existing utilities. Depending on how synced you want the aliases to be you could source the list in .bashrc or prior to each command line with PROMPT_COMMAND:


aliasutil add "gcm" "git checkout master"

echo 'alias gcm="git checkout master"' >> $ALIAS_FILE

aliasutil add "gfp %1" "git push --force %1"

Do you intend the %1 as an argument? Aliases do not support arguments, so to achieve this you need to create a function.

aliasutil list


aliasutil remove "gcm"

sed -i '/^alias gcm=/d'
unalias gcm
share|improve this answer
The sed script for deleting needs to be sed -i '/^alias gcm=/d' – cas Sep 7 '12 at 13:59
Your right, thanks. – Thor Sep 7 '12 at 14:02
you're welcome. but it still needs the = to anchor the end of the match :) otherwise it'll delete all aliases beginning with gcm, not just gcm. – cas Sep 7 '12 at 14:05
Indeed, I was too quick. – Thor Sep 7 '12 at 14:09

Your Answer


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.