Issue
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?
Solution
!
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 withset +H
Answered By - janos