|
Home > Archive > Unix Shell > October 2005 > bash: Can't use an alias within a function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
bash: Can't use an alias within a function
|
|
| Ronald Fischer 2005-10-24, 3:45 pm |
| Is there a way to trick bash into expanding aliases
within a function? I've tried expand_aliases, but it
doesn't help. Here is an example code:
========================================
================
function xxdef()
{
shopt -s expand_aliases
alias ali1="echo I am ali1"
}
alias ali2="echo I am ali2"
function xxuse()
{
shopt -s expand_aliases
ali1
ali2
ali3
}
alias ali3="echo I am ali3"
xxdef
# At this point, the aliases ali1, ali2, ali3 are defined:
echo test1....
ali1
ali2
ali3
# Now try to call them from within xxuse:
echo test2....
xxuse
========================================
====================
The output from this script is:
test1....
I am ali1
I am ali2
I am ali3
test2....
../xxdefines: line 18: ali1: command not found
../xxdefines: line 19: ali2: command not found
../xxdefines: line 20: ali3: command not found
Of course I know that I should NOT use aliases at all,
but functions instead, but assuming that this is not
an option: Is there a way in bash to use aliases, which
are defined outside a function, inside that function?
Ronald
--
Sent by mn-pg-p-e-b-consultant-3.com from siemens in field com
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-replayer.com
| |
| Stephane Chazelas 2005-10-24, 3:45 pm |
| On Fri, 21 Oct 2005 09:41:48 GMT, Ronald Fischer wrote:
> Is there a way to trick bash into expanding aliases
> within a function? I've tried expand_aliases, but it
> doesn't help. Here is an example code:
>
> ========================================
================
> function xxdef()
> {
> shopt -s expand_aliases
> alias ali1="echo I am ali1"
> }
>
> alias ali2="echo I am ali2"
>
> function xxuse()
> {
> shopt -s expand_aliases
> ali1
> ali2
> ali3
> }
[...]
Note that the correct way to define a function is
xxuse()
{ ...
function xxuse () is a kind of hybrid between the Bourne way and
the ksh way. There's no need to had yet another prototype.
The alias expansion is done at an early stage in the parsing.
When you run a function, the parsing has already been done when
the function code was read.
You need either to set expand_aliases and to defined the aliases
before defining the functions:
shopt -s expand_aliases
alias ali2="echo ali2"
xxuse() {
ali2
}
xxuse
Or to use "eval" to delay the parsing until the running of the
function:
xxuse() {
alias foo="echo bar"
eval foo
}
shopt -s expand_aliases
xxuse
Best is not to use aliases at all, that's not a feature intended
for non-interactive use.
Use functions:
xxuse() {
foo() { echo bar; }
foo
}
xxuse
--
Stephane
| |
| Chris F.A. Johnson 2005-10-24, 3:45 pm |
| On 2005-10-21, Ronald Fischer wrote:
> Is there a way to trick bash into expanding aliases
> within a function?
[snip]
> Of course I know that I should NOT use aliases at all,
> but functions instead, but assuming that this is not
> an option:
When are functions not an option?
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
| |
| ro.naldfi.scher@gmail.com 2005-10-24, 3:45 pm |
| When the alias definitions are part of a framework which you want to
use, but the framework is not maintained by yourself.
| |
| Chris F.A. Johnson 2005-10-24, 3:45 pm |
| On 2005-10-24, ro.naldfi.scher@gmail.com wrote:
> When the alias definitions
What alias definitions?
This is Usenet, not a web forum (though it is also bastardized on
several web sites). You cannot know whether the reader can see or
has seen the previous posts, or, if they have been seen, whether
the reader remembers what they were about.
When using groups.google.com to reply to a Usenet article (better
to use a real newsreader), click on "show options" at the top of
the article, then click on the "Reply" at the bottom of the
article headers. This will quote the previous message in the
accepted manner. Trim the parts of it that are not relevant to
your follow-up.
> are part of a framework which you want to
> use, but the framework is not maintained by yourself.
Aliases cannot be exported, so you cannot use them if they are in
another script.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
| |
| Dan Mercer 2005-10-25, 2:49 am |
|
<ro.naldfi.scher@gmail.com> wrote in message news:1130136155.120838.297530@z14g2000cwz.googlegroups.com...
: When the alias definitions are part of a framework which you want to
: use, but the framework is not maintained by yourself.
:
In bash you must
shopt -s expand_aliases
to use aliases in a non-interactive shell - i.e. scripts. Aliases can be used in functions -
it is a common practice to alias cd to a function, then use cd in other functions
that call the alias, not the real command:
$ chdir() { \cd "$@";PS1=".../${PWD##*/}${IFS#??}$ "; }
$ alias cd=chdir
$ lbin() { cd /usr/local/bin; }
$ lbin
.../bin
$
This is, of course, a trivial example. Real examples might keep lists of
directories traversed, or might look for the presence of a variable
like $NEWUSER to display the contents of the file .newuser when a
new directory has been entered (applications I've seen).
So what's your real problem? Your question was rather vague.
Dan Mercer
| |
| ro.naldfi.scher@gmail.com 2005-10-25, 2:49 am |
|
Chris F.A. Johnson schrieb:
> When using groups.google.com to reply to a Usenet article (better
> to use a real newsreader),
Yes, this would really be better - just that it isn't
available everywhere.
> click on "show options" at the top of
> the article, then click on the "Reply" at the bottom of the
> article headers.
Thank you!!!! In the earlier version, Google did this
automatically. I was not aware this usage of the "options"
link, it is really helpful.
Ronald
| |
| ro.naldfi.scher@gmail.com 2005-10-25, 2:49 am |
| > In bash you must
>
> shopt -s expand_aliases
>
> to use aliases in a non-interactive shell
Yes, this is what I'm finally ending up with...
Thank you all for helping.
Ronald
| |
| Stephane CHAZELAS 2005-10-25, 7:48 am |
| 2005-10-25, 03:49(+00), Dan Mercer:
>
> <ro.naldfi.scher@gmail.com> wrote in message news:1130136155.120838.297530@z14g2000cwz.googlegroups.com...
> : When the alias definitions are part of a framework which you want to
> : use, but the framework is not maintained by yourself.
> :
>
> In bash you must
>
> shopt -s expand_aliases
>
> to use aliases in a non-interactive shell - i.e. scripts. Aliases can be used in functions -
> it is a common practice to alias cd to a function, then use cd in other functions
> that call the alias, not the real command:
>
> $ chdir() { \cd "$@";PS1=".../${PWD##*/}${IFS#??}$ "; }
> $ alias cd=chdir
> $ lbin() { cd /usr/local/bin; }
[...]
But note that this is the same as
lbin() { chdir /usr/local/bin; }
i.e., if you unalias cd later one, lbin will continue calling
chdir because the alias is expanded at the time lbin definition
is parsed.
(note that with shells like bash or zsh, you don't need all that
as those shells have PS1 escape sequences for the basename of
the current directory).
--
Stéphane
| |
| Dan Mercer 2005-10-25, 8:49 pm |
|
"Stephane CHAZELAS" <this.address@is.invalid> wrote in message news:slrndlrpl0.5l4.stephane.chazelas@spam.is.invalid...
: 2005-10-25, 03:49(+00), Dan Mercer:
: >
: > <ro.naldfi.scher@gmail.com> wrote in message news:1130136155.120838.297530@z14g2000cwz.googlegroups.com...
: > : When the alias definitions are part of a framework which you want to
: > : use, but the framework is not maintained by yourself.
: > :
: >
: > In bash you must
: >
: > shopt -s expand_aliases
: >
: > to use aliases in a non-interactive shell - i.e. scripts. Aliases can be used in functions -
: > it is a common practice to alias cd to a function, then use cd in other functions
: > that call the alias, not the real command:
: >
: > $ chdir() { \cd "$@";PS1=".../${PWD##*/}${IFS#??}$ "; }
: > $ alias cd=chdir
: > $ lbin() { cd /usr/local/bin; }
: [...]
:
: But note that this is the same as
:
: lbin() { chdir /usr/local/bin; }
:
: i.e., if you unalias cd later one, lbin will continue calling
: chdir because the alias is expanded at the time lbin definition
: is parsed.
Which reminds me of another point - for aliases to be used by functions
they must be defined before the function is instantiated. (For bash and
Bourne sh, that's when the function is defined. For ksh, that may be for
FPATH'd functions the first time it's called).
:
: (note that with shells like bash or zsh, you don't need all that
: as those shells have PS1 escape sequences for the basename of
: the current directory).
The above was offered merely for illustrative reasons, not as practical code.
Dan Mercer
:
:
: --
: Stéphane
|
|
|
|
|