I have been trying to make aliases work in bash shell. Now, let's say I do alias yum='yum -v' in my shell. It works when I run the run the command from CLI. But when I run a script it doesn't have any effect. How can I make the aliases work across the shell?
-
2You can make the script source your `~/.bashrc` or any file containing these aliases. – fedorqui Mar 14 '14 at 11:48
-
Tried it, not working! – Pensu Mar 14 '14 at 11:54
-
It should. See [using alias in shell script](http://stackoverflow.com/questions/15968053/using-alias-in-shell-script) – fredtantini Mar 14 '14 at 11:58
-
1@fredtantini Alias expansion is disabled by default in non-interactive shells. – chepner Mar 14 '14 at 14:17
-
@chepner Thanks, I didn't know that. When running a script with `. /home/fti/.bash_aliases`\n `ll` in ksh, my `ll` bash alias was executed… – fredtantini Mar 14 '14 at 15:27
-
Well, it's not really a `bash` alias, since you source the file in `ksh`. – chepner Mar 14 '14 at 15:29
2 Answers
In bash, you can export functions, so if you do:
yum() { command yum -v "$@"; }
export -f yum
Then the 'alias' for yum will persist in subshells. Note that functions are almost always preferred to aliases, and (from the bash man page) "For almost every purpose, aliases are superseded by shell functions.". I believe the initial clause 'For almost every purpose' is merely hyberbole and that 'almost' can be safely omitted.

- 204,365
- 48
- 270
- 300
From the bash
manpage:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).
As a result, you need to do the following inside your script.:
shopt -s expand_aliases
You will also need to source your file with aliases (.bashrc
,.bash_profile
,.bash_login
etc).
shopt -s expand_aliases
source /path/to/alias/file
Please note that using alias
inside your shell script will make it less portable as it will break when ran on environment that do not have your aliases.

- 74,723
- 23
- 102
- 147