[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Incorrect alias expansion within command substitution
From: |
Chet Ramey |
Subject: |
Re: Incorrect alias expansion within command substitution |
Date: |
Tue, 1 Feb 2022 10:23:53 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 |
On 1/31/22 11:40 PM, Martijn Dekker wrote:
On the latest code from the devel branch:
GNU bash, versie 5.2.0(35)-alpha (x86_64-apple-darwin18.7.0)
Reproducer script:
shopt -s expand_aliases
alias let='let --'
set -x
let '1 == 1'
: $(let '1 == 1')
Output:
+ let -- '1 == 1'
++ let -- -- '1 == 1'
foo: line 5: let: --: syntax error: operand expected (error token is "-")
+ :
The alias is incorrectly expanded in the command substitution, duplicating
the '--' argument and causing a syntax error.
Thanks for the report. This is a consequence of parsing the contents of the
command substitution `early', at parse time, then doing it again at
execution time. It's clearly wrong to expand aliases during both passes.
But which is the `right' time? If you expand on the first pass, you get
constructs like this failing to find command `a':
[ -n "$BASH_VERSION" ] && shopt -s expand_aliases
foo()
{
var=$(a b)
echo $var
}
alias a=echo
foo
If you defer alias expansion until execution, you lose the (posix-
encouraged but officially unspecified according to the approved
interpretation of issue 1342) ability to have aliases affect command
parsing in the command substitution:
[ -n "$BASH_VERSION" ] && shopt -s expand_aliases
alias nest='('
echo $( nest echo ok 1 ) )
echo "$( nest echo ok 2 ) )"
Can't have both.
Historically, bash (and ksh93) has favored the former. Just about all the
other shells claiming some sort of POSIX conformance favor the latter (all
the ash-based shells, yash, mksh).
What are your plans here?
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
Message not available