Smaller example (but probably not smallest), in Bash 4.1:
$ set -H
$ arr=( "$(echo '1 && !/==/' )" )
bash4.1: !/==/': event not found
$ arr=( $(echo '1 && !/==/' ) ) # no error
$
and in Bash 4.4:
$ set -H
$ arr=( "$(echo '1 && !/==/' )" ) # no error
$ arr=( $(echo '1 && !/==/' ) ) # no error
$
That's history expansion allright. It happens quite early in the command line processing, and doesn't parse the whole command before that:
History expansion is performed immediately after a complete line is read, before the shell breaks it into words, and is performed on each line individually.
No idea why it works like that, why the double quoting makes a difference, but it's not the only bug with history expansion I remember hearing about.
You can turn history expansion off with set +H
, or set +o histexpand
, and it's not enabled in scripts by default anyway.
Also note that Bash 4.1 is more than 10 years old now, even Bash 4.4 (the last with a 4.x version number) was released in 2016. Other bugs have been fixed since 4.1, too, as well as useful features added.
don't
). Can you post an exact example that shows the problem (along with the exact error message you get)? Also, what version of bash are you using (echo "$BASH_VERSION"
will tell you)? – Gordon Davisson Feb 13 '21 at 00:43!/pat/
by(/pat/==0)
so that atleast history expansion error goes away. Then see what you get. – guest_7 Feb 13 '21 at 00:45