A double-quoted multi-line string starts history expansion on subsequent single-line commands that it passes

I am in GNU bash version 4.3.11.

Say I want to print unique lines in a file. I use this approach, which works well in a file:

$ cat a 9 10 9 11 $ awk '!seen[$0]++' a 9 10 11 

However, if I get input from stdin using double quotes in multi-line and piping to awk, this fails:

 $ echo "9 > 10 > 9 > 11" | awk '!seen[$0]++' bash: !seen[$0]++': event not found 

That is, bash is trying to deploy the seen command, which of course does not know, because it is the name of the variable. But this should not happen, because the command is placed in single quotes.

echo on single-line multi-line input works well:

 $ echo '9 > 10 > 9 > 11' | awk '!seen[$0]++' 9 10 11 

The funny thing is that it also works well on single-line input, which is quoted twice:

 $ printf "9\n10\n9\n11" | awk '!seen[$0]++' 9 10 11 

I wonder why bash tries to expand the story if it occurs after mutiline is entered, although the command itself uses single quotes.

Other considerations:

Having a pipe between them, she does not correct:

 $ echo "9 > 10 > 9 > 11" | cat - | awk '!seen[$0]++' bash: !seen[$0]++': event not found 

And setting set +H disables the story , so it works well because it is not trying to expand anything:

 $ set +H $ echo "9 > 10 > 9 > 11" | awk '!seen[$0]++' 9 10 11 

I reviewed the rici canonical answer on how to address the error "bash :! d ': event not found" in bash command substitution and found many possible causes, but none of them matches this behavior.

+7
bash gnu command-substitution
source share
1 answer

It's not a mistake.

After querying on the bash-bugs mailing list, I got the following response:

The extension of the story is clearly line-oriented.

He does not know about the state of the shell, especially the state of quoting the shell, which covers the lines.

He knows about an undefined shell quote that comes across a large set of Unix utilities — starting with history and reading libraries used outside the shell — and that the double quote introduces a quoted string in which single quotes are not significant and do not prevent the story from expanding.

+2
source share

All Articles