[Top][All Lists]

[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: Sat, 5 Feb 2022 13:10:15 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.4.1

On 2/4/22 4:59 PM, Alex fxmbsw7 Ratchev wrote:
On Fri, Feb 4, 2022 at 10:18 PM Robert Elz <kre@munnari.oz.au> wrote:

     Date:        Fri, 4 Feb 2022 21:06:11 +0100
     From:        Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com>

   | now changing this to dynamic eof marker

There is no such thing.   Unlikely (*VERY* unlikely) there ever will be.

   | cat <<$( printf leet )
   | $(  printf leet )
   | $( printf leet )
   | notice the two spaces in the middle to not match eof

Yes, that is supposed to work, and "cat" should write a single
line containing "leet".   The end word is *never* expanded, not
the copy of it after the << operator, and not the one that terminates
the here doc data (the former gets quote removal, but there are none
here to remove).

i guess thats not rendundant enough for bash, or yet too early
cause it doesnt interpret at runtime much yet, it just maps text or
chars to read until

What does `not redundant enough' mean here?

also its a high security risk, how to say... to let $( and all << as
separator is all nonsense

How, exactly? kre already said it doesn't cause any command execution.

written twice.. to match eof matching, but why
if you start your << with $( printf leet ) or $(  printf leet ) and
just wanna end with 'leet' .. .. .. you need command subst ( aka same
code ) to match instead of the resulting text ?

Why would you write something that way? Or, more to the point, why would
you have that expectation?

   | but
   | cat <<$(  printf end )
   | $( printf end )
   | # here it should already end the heredoc parser

No it shouldn't.   The 2nd line there doesn't have enough spaces,
they're not the same.

i didnt continue the last line that matched the same command substitution
it was a try to example from a view of interpreting the text dynamically
still a bit unclear to me

You still want the here-doc delimiter to undergo word expansion? And all
the lines in the here-document body? Before checking for the matching
delimiter. Yeah, that's a mess.

The end word (after <<) is parsed as a command substitution, to find its
end, but is never expanded (the code in it is never executed) - all the
parsing accomplishes in this case is to allow the correct ')' to be found
so the command substitution text ends at the correct place - beyond that
it is ignored.

what are you saying isnt clear to me
do you want $( to expand or not


Perhaps a different example - I'm sure you know that in $(( )) you can
embed an arithmetic expression, and if you try and do something like

         $(( data 2 ))

you'll get a syntax error, because "data 2" is not a valid arithmetic

you get this cause alias parsing is disabled, cause $(( is a command
an alias $(( would work

It would not. I know you know how alias expansion works, but here are the
three keys: it happens on a per-token basis, it happens only when a token
is in a command position or an immediately-previous alias expansion ends
with a space, and it doesn't happen in a quoted string. Keep in mind that
the contents of an arithmetic expansion are treated as a double-quoted

Oh, and `$((' is not a command. A token in a command position might have
an alias expansion that results in `$((', but that's not the same thing.

but bash doesnt handle the ending right yet

Please show your work here.

hint to chet: its just sad it results yet early in a code parse error

#!/usr/bin/env -S bash

shopt -s expand_aliases

alias -- \
p='printf %s\\n ' \
assign='assign=$(( ' begin='$(( ' end=')) ' \
sep=', ' \
plus=+\  minus=-\  div=/\  mul=\*\  \

for d in "1 + 2" "1 plus 2"
alias -- data="$d "
p begin data end

nevermind i realized its exactly the same and i understood why its erroring

alias.sh: line 14: unexpected EOF while looking for matching `)'
alias.sh: line 16: syntax error: unexpected end of file

cause bash doesnt parse aliases yet into pre parse state

yea i just wish it would, such as no code error for valid code

Sigh. It's not valid code, and I hope you understand why it's not.

``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/

reply via email to

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