[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] Using aliases or functions in bash script



T o n g wrote:
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)'

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.


Reply to: