On Thu, Sep 27, 2007 at 05:23:05PM +0100, David Reynolds wrote:
On 27 Sep 2007, at 11:36 am, Chris G wrote:
How do you get aliases to work in bash shell scripts?
At the moment the only way I can get an alias that I have defined in my .bashrc file to work in a script is to explicitly source .bashrc in the shell script. Is this the only way it can be done?
The korn shell can do 'alias -x fred=bert' which means that fred will work as an alias for bert in scripts called from a command line ksh where the alias is in effect. Is there no way that bash can export aliases so they work in scripts?
A quick Google search suggests that bash really can't export its aliases, which is a pity. Does anyone know otherwise?
Have you looked at the bash man page?
Yes, definitely, and searched for all the occurrences of 'alias'.
This seems relevant:
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).
That's what I thought too but it doesn't mean what I thought it meant.
Saying 'shopt -s expand_aliases' either in my .bashrc or even in the shell script where I want them to work doesn't have any effect. The alias simply doesn't work in the script unless you actualy execute .bashrc from the script.