0

I am trying to echo a shebang to a file.

vincent@vincent-X751LJ:~$ echo "#!/usr/bin/sh" > file
echo "#/usr/bin/sh" > file
vincent@vincent-X751LJ:~$ cat file
#/usr/bin/sh

The "bang" vanishes (even in my history !). Everything looks like I never typed !. I have also tried without the double quotes. Same result.

It does not happen if I echo just #!

vincent@vincent-X751LJ:~$ echo "#!" > file
vincent@vincent-X751LJ:~$ cat file
#!

The reason is certainly so simple...

The only reference to a shebang echoing problem is on SO (How to echo a shebang in cmake COMMAND) but the problem is different on my side.

Thanks

zigma12
  • 181
  • 1
  • 7
  • 1
    I'm not sure why this happens exactly, but using single quotes (to disable shell escaping) should remedy this. – Alexander Aug 02 '22 at 19:46
  • @Alexander: thanks. It works. I did not know simple and double quotes behaved differently. – zigma12 Aug 04 '22 at 19:40
  • It happens because the ! bang character in bash is a signal for a history search. `echo !3` will echo whatever the 3rd command in the history is. – Tim Roberts Aug 04 '22 at 20:53

1 Answers1

0

I'm not sure why this happens exactly, but using single quotes (to disable shell escaping) should remedy this.

See Difference between single and double quotes in Bash

Alexander
  • 59,041
  • 12
  • 98
  • 151