6

I have tried the escape character but it doesn't seems works with !. I need this to auto-configure packages using scripts.

Seff
  • 1,546
  • 1
  • 17
  • 19

3 Answers3

13

! is magic in a default interactive bash session, but not in scripts (set +H to disable in an interactive prompt). In any case:

echo '#!/bin/bash'
that other guy
  • 116,971
  • 11
  • 170
  • 194
5

Or with no quotes and escaping the # and the !:

echo \#\!/bin/bash
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
1

printf should work too:

printf '#!/bin/bash\n'
Avi Das
  • 1,725
  • 1
  • 13
  • 19
  • 1
    `printf` is beside the point. The issue is the shell's interpreting of the hashmark, not which command it's passed to. (Also, if you're going to use `printf`, you'll want to include a `\n` at the end, or your shebang line will run into the next line printed. – Adam H. Peterson Apr 01 '14 at 23:48
  • (Correction: I meant bang, rather than hashmark.) – Adam H. Peterson Apr 02 '14 at 00:14
  • Whoops you are right, it does need the \n – Avi Das Apr 02 '14 at 07:11
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – ElmoVanKielmo Apr 02 '14 at 07:18
  • @ElmoVanKielmo It does provide an answer to the question, though the issue OP was having wasn't really related to `echo/printf` so it's basically the same as [that other guy's](http://stackoverflow.com/a/22797555/3076724) – Reinstate Monica Please Apr 02 '14 at 23:26
  • @BroSlow this was a review and as you probably know - messages are predefined in that case. – ElmoVanKielmo Apr 03 '14 at 05:25