In a script, the characters #!
are not special in this context. The snippet printf -- "#!${opt_E}"
calls the printf
command with two arguments: --
, and #!
concatenated with the value of the opt_E
variable. The argument --
tells printf
that even if there are subsequent arguments beginning with -
, they are not to be interpreted as options; it doesn't make a difference here since #!${opt_E}
doesn't begin with -
. The double quotes around #!${opt_E}
protect #
from being interpreted as a comment start character, and they protect the value of opt_E
from being split into separate words which are interpreted as wildcard patterns.
If the value of opt_E
doesn't contain any %
or \
character, then this command prints #!
followed by the value of opt_E
, with no final newline. In general, the command interprets the value of opt_E
as a printf format.
If you try this out in an interactive shell, you may see strange effects due to !
being interpreted as a history expansion character, which automatically recalls previous commands. To avoid this, add a \
before !
. !
is also interpreted literally within single quotes: printf -- '#!${opt_E}'
.
If you're replaying a script, you'll have to have set opt_E
to the right value first. If you're trying to debug a script, add set -x
on the second line (insert it just below the initial #!
line): the shell will print a trace of each line as it executes it.
!
is reinterpreted by Bash, so try usingdash
or some other shell that doesn't do history expansion. – sr_ Jan 21 '13 at 09:51printf '#!%s' "$opt_E"
BTW – Stéphane Chazelas Jan 22 '13 at 21:47