Domanda

Long story short, is it possible to make bc evaluate the following expression?

echo "!(2.2 >= 0) && !(2.2 < 10)" | bc
bash: !: event not found

I know this works:

echo "(2.2 >= 0) && (2.2 < 10)" | bc
1

So what am I doing wrong? The bc man page said that it has support for !expr, but I cannot seem to invoke it. I know it's possible to work around the issue by negating the comparison signs, but I'd like to know how should the !expr be invoked in this case.

Much appreciated.

È stato utile?

Soluzione

bash: !: event not found

There is a feature of bash, called "history expansion" (see man bash): it handles ! specially, even within double quotes.

Replacing double quotes with single quotes should help (history expansion doesn't happen there).

Also, history expansion doesn't happen in non-interactive shells (that is, your example will work as is in a script).

Altri suggerimenti

It seems like the bash is interpreting your "!" as some kind of command. Replacing " for ' has worked for me.

It also happens when just outputing something to the terminal:

$ echo "!"
-bash: !: event not found
$ echo '!'
!

a walk-around in bash with double quote:

kent@7pLaptop:/tmp$ echo  -e "\x21(3>5)"|bc
1

tested under:

kent@7pLaptop:/tmp$ bash -version|head -1
GNU bash, version 4.2.42(2)-release (i686-pc-linux-gnu) 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top