bug-bash
[Top][All Lists]
Advanced

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

Re: Alias and if


From: Chet Ramey
Subject: Re: Alias and if
Date: Sun, 04 Dec 2005 17:21:02 -0500
User-agent: Thunderbird 1.5 (Macintosh/20051025)

XIAO Gang wrote:
> 
> Version: GNU bash, version 3.00.16(1)-release (i386-pc-linux-gnu)
> 
> Hardware: AMD Athlon 3GHz+, 1G RAM.
> 
> OS: Linux (Knoppix-3.9).
> 
> Compilation: Distributed by Debian.
> 
> Description: An alias defined within an "if" branch has no effect.
> 
> Recipe: in the following script, the alias "mycp" is unknown command.
> The problem disappears when
> the line "alias myls=ls" is moved before the "if" line.
> 
> Note that the definition is however recorgnized by "alias -p".
> 
> The same phenomenon is reported on slightly different recent builds of
> Linux.
> 
> ####################################
> 
> #! /bin/bash
> 
> if true; then
> alias myls=ls
> alias -p
> myls
> fi
> 
> myls
> 
> ####################################

Bash always reads a complete command before executing any portion of
it (it has to know that it has a complete command, for example).
Aliases are expanded when a command is read, not when it is executed.
In this case, that means that the `myls' alias is not defined until
the body of the if statement executes, but alias expansion has already
occurred while that command was being parsed.

The bash man page alludes to this when it talks about commands
appearing on the same line as an alias definition.  To be more
precise, it should state the conditions for alias expansion in terms
of simple and compound commands.

I'm assuming that you already know that aliases are not expanded in
non-interactive shells unless the `expand_aliases' option is enabled
or the shell is in posix mode; your report implies it.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                                Live Strong.
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]