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.