Using aliases or functions in bash script 

Newsgroups: gmane.linux.debian.user
Date: Sat, 26 Jan 2008

An advanced bash alias expansion question — How can I use my aliases or functions in my bash script?

I have the following alias and function *already* defined in my ~/.bashrc:

$ bash
$ alias rd
alias rd='rmdir'
$ type dt
dt is a function
dt ()
{
    pushd +$1
}
$ exit
exit

I.e., there are there in the interactive shell. Now I want to use them in my script, How can I use them in my script?

Looking through the man pages, I think the following content is related to my question:

Aliases  are not expanded when the shell is not interactive, unless the
expand_aliases shell option is set using shopt.

O [shopt_option]
                 shopt_option  is  one  of  the  shell options accepted by the
                 shopt  builtin  (see  SHELL  BUILTIN  COMMANDS  below).    If
                 shopt_option is present, -O sets the value of that option; +O
                 unsets it.  If shopt_option is not supplied,  the  names  and
                 values  of the shell options accepted by shopt are printed on
                 the standard output.  If the invocation  option  is  +O,  the
                 output  is displayed in a format that may be reused as input.

expand_aliases
        If set, aliases are expanded as  described  above  under
        ALIASES.  This option is enabled by default for interac-
        tive shells.

And this is what I've tried:

$ bash -c 'shopt -s expand_aliases; alias rd'
bash: line 0: alias: rd: not found
$ bash -O expand_aliases -c 'rd /tmp/ttt; alias rd; dt bin; type dt'
bash: rd: command not found
bash: line 0: alias: rd: not found
bash: dt: command not found
bash: line 0: type: dt: not found

I even tried the following but it didn't work either:

$ bash -O expand_aliases -c '. ~/.bashrc; (rd /tmp/ttt; alias rd; dt bin; type dt)'

Solution 

> Use functions. Don't use aliases.

This sounds like "Don't use #define in C" to me. Despite its limitations, why one can't use aliases in scripts?

xpt

Note: no answer.

Solution 

> An advanced bash alias expansion question --
> How can I use my aliases or functions in my bash script?
> PS. I even tried the following but it didn't work either:
>
>   $ bash -O expand_aliases -c '. ~/.bashrc; (rd /tmp/ttt; alias rd; dt bin; type dt)'
$ cat .bashrc
alias rd="echo foo"
$ bash -c 'alias rd'
bash: line 0: alias: rd: not found
$ bash -c '. ~/.bashrc; alias rd'
alias rd='echo foo'
$ bash -c '. ~/.bashrc; (alias rd)'
alias rd='echo foo'

As far as I can see, your last attempt should have worked.

The point of my previous response, which I complete failed to make (and in fact didn't realize until after I'd sent it…) is that your .bashrc probably includes a line of the form:

[ -z "$PS1" ] && return

So that the bashrc returns prior to defining any aliases. For example:

$ cat .bashrc
alias rd="echo foo"
$ bash -c '. ~/.bashrc; alias rd;'
alias rd='echo foo'
$ cat > .bashrc << 'EOF'
 > [ -z "$PS1" ] && return
 > alias rd="echo foo"
 > EOF
$ bash -c '. ~/.bashrc; alias rd;'
bash: line 0: alias: rd: not found
$ bash -c 'PS1="."; . ~/.bashrc; alias rd;'
alias rd='echo foo'

Another solution is to put the function and alias definitions before the check for the interactive shell. I think that a better solution is to define PS1 to fool the test as above, but I don't think that is a particularly good solution either. Probably better would be to move the function/alias definitions into a .bash_functions file, source that from .bashrc and from any scripts that want the functions and aliases.

William Pursell @gmail.com

Solution 

>> An advanced bash alias expansion question --
>> How can I use my aliases or functions in my bash script?
>
>> PS. I even tried the following but it didn't work either:
>>
>>   $ bash -O expand_aliases -c '. ~/.bashrc; (rd /tmp/ttt; alias rd; dt bin; type dt)'
>
> . . . your .bashrc probably includes a line of the form:
> [ -z "$PS1" ] && return
>
> So that the bashrc returns prior to defining any aliases. . .

OMG, that's EXACTLY the reason!

For NEARLY THREE YEARS[1] has the question been haunting me, and you solve it, by identifying my culprit line via looking through a crystal ball :-).

Thanks a million William!!!

[1] http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-06/0416.html

xpt

Solution 

Trying History 

The alias def and its usage should not be on the same line:

$ cat test.sh
. ~/bin/.aliases_bs; alias rd; shopt -s expand_aliases; rd -v /tmp/ttt
$ bash test.sh
alias rd='rmdir'
test.sh: line 1: rd: command not found

'alias rd' shows that the alias has been defined, but invocating it still fails.

$ cat test.sh
. ~/bin/.aliases_bs; alias rd
rd -v /tmp/ttt
$ bash test.sh
alias rd='rmdir'
rmdir: removing directory, /tmp/ttt

— Putting the alias def and its usage on different line solved the problem.

But better yet, use BASH_ENV, especially when using the bash commandline:

mkdir /tmp/ttt
$ BASH_ENV=~/bin/.aliases_bs bash -c "rd -v /tmp/ttt"
rmdir: removing directory, /tmp/ttt

documented on: 2008-01-27