Using aliases or functions in bash script



Hi,

A (adv) bash alias expansion question --
How can I use my aliases or functions in my bash script?

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

$ alias rd
alias rd='rmdir'

$ type dt
dt is a function
dt ()
{
pushd +$1
}

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

Looking for the answer myself, I found a similar symptom has been reported
as bug 148505: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=148505
And it is reported as being fixed.

Am I hitting a new bug or I'm doing something wrong? Please comment,
otherwise I'm going to file a bug report.

Thanks

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)'

--
Tong (remove underscore(s) to reply)
http://xpt.sourceforge.net/techdocs/
http://xpt.sourceforge.net/tools/


--
To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx
with a subject of "unsubscribe". Trouble? Contact listmaster@xxxxxxxxxxxxxxxx



Relevant Pages

  • Re: [OT] Using aliases or functions in bash script
    ... How can I use my aliases or functions in my bash script? ... I have the following alias and function defined in my ~/.bashrc: ... shopt builtin. ... values of the shell options accepted by shopt are printed on ...
    (Debian-User)
  • Re: csh to bash
    ... Clearly you don't understand what the original alias does in csh. ... every file preceded by the filename if you invoke it without ... The basic rule is "never use aliases with bash, ksh, etc. always use ...
    (comp.unix.shell)
  • [OT] Using aliases or functions in bash script
    ... and the two replies are the only replies that I ... An advanced bash alias expansion question -- ... How can I use my aliases or functions in my bash script? ... values of the shell options accepted by shopt are printed on ...
    (Debian-User)
  • Re: Handy Bash alias script
    ... Aliases don't support arguments *at all* in bash. ... a shell function should be used (see FUNCTIONS ... alias deps='apt-cache showpkg $1' ...
    (Debian-User)
  • Re: csh to bash
    ... Clearly you don't understand what the original alias does in csh. ... if you invoke it without one. ... The basic rule is "never use aliases with bash, ksh, etc. always use ...
    (comp.unix.shell)