Bash – How to write a bash script that sets up aliases for the parent shell

aliasbash

I'm trying to write a script that will set up aliases for my bash shell, but I don't want to source it automatically in .bashrc – I need to have the aliases in a subset of my terminals.

Is it possible to alias a command in a script and have the aliased command work for the shell the script was run from?

Desired functionality:

$ alias
# ... no output here
$ ./my-script
$ alias
alias foo='bar'
alias alpha='beta'
...

Best Answer

Aliases are private to the shells that they're created in. They can neither be exported, nor can they be accessed from a parent shell.

The easiest solution is to break the aliases out into a separate file, as you suggest, then either source that file by hand, or add a function to your .bashrc, that will source them when invoked.

function extra-aliases {
     . /path/to/file/containing/additional/aliases
}