Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-bash: !/usr/bin/env: event not found [duplicate]

I got this strange error message trying to write shebang to a python script:

$ echo "#!/usr/bin/env python" > scripts/sandbox.py 
-bash: !/usr/bin/env: event not found

$ echo "say what?" > scripts/sandbox.py

Immediately doing the same thing but without using shebang line works. What is this behavior and how can it be overcome?

like image 397
codyc4321 Avatar asked Nov 30 '25 14:11

codyc4321


1 Answers

! is a special character to bash, it is used to refer to previous commands. It is expanded within double-quotes. To avoid that, enclose them in single-quotes instead:

echo '#!/usr/bin/env python' > scripts/sandbox.py 

@mklement0 clarified it beautifully in a comment:

More specifically, ! is special to Bash's history expansion feature, which is on by default (only) in interactive shells. In addition to avoiding it by using single-quoted strings, it can be turned off altogether with set +H

like image 72
janos Avatar answered Dec 02 '25 04:12

janos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!