I need to printf a simple script and redirect the output to a file, but when I do this:
printf "#!/bin/bash\ntouch /tmp/1234567890_$RUN" > /tmp/password-change-script_$RUN.sh
I get this error:
bash: !/bin/bash\ntouch: event not found
If I escape the exclamation mark:
printf "#\!/bin/bash\ntouch /tmp/1234567890_$RUN" > /tmp/password-change-script_$RUN.sh
Then the escape character is still present in the file.
cat /tmp/password-change-script_$RUN.sh
#\!/bin/bash
touch /tmp/1234567890_111
By the way, in this particular case, the #!/bin/bash MUST be in the file. For some reason the binary file that executes the script won't read the file otherwise.
Bash maintains the history of the commands executed in the current session. We can use the exclamation mark (!) to execute specific commands from the history.
In addition to using single quotes for exclamations, in most shells you can also use a backslash \ to escape it.
The exclamation mark is part of history expansion in bash. To use it you need it enclosed in single quotes (eg: 'http://example.org/!132' ). You might try to directly escape it with a backslash ( \ ) before the character (eg: "http://example.org/\!132" ).
It simply prints out the result of the substitution (which is the command it is about to execute) and than the result of the commands execution. @Danagon I think I might have misperceived the question and my answer may not apply, but history expansion still applies here.
The !
character is expanded in double-quoted strings, but not in single-quoted strings.
printf '#!/bin/bash\ntouch /tmp/1234567890_'"$RUN"
It's also not expanded when it appears by itself or at the end of a word; this isn't as clean but:
printf "#%c/bin/bash\ntouch /tmp/1234567890_$RUN" !
You can also temporarily turn off history substitution by (temporarily) setting $histchars
to the empty string; this turns off the special treatment of !
:
histchars=
printf "#!/bin/bash\ntouch /tmp/1234567890_$RUN"
unset histchars
Or you can execute the printf
command in a script rather than interactively (history substitution is on by default only for interactive shells).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With