Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am not able to understand this command and getting confused : here are things i executed on linux trying to undertand its working

[root@testgfs2 final_scripts]# printf -- "#!${opt_E}"
printf -- "#reset{opt_E}"
#reset{opt_E}[root@testgfs2 final_scripts]# printf -- "#!${opt_E}"
printf -- "#"#reset{opt_E}"{opt_E}"
##reset{opt_E}{opt_E}[root@testgfs2 final_scripts]# echo !$
echo "#"#reset{opt_E}"{opt_E}"

how does this work and i don't know under what topic it comes so i am unable to find it on google also.

also what does -- doing after printf

share|improve this question
See What does “--” (double-dash) mean?‌​. –  manatwork Jan 21 '13 at 9:50
Note that ! is reinterpreted by Bash, so try using dash or some other shell that doesn't do history expansion. –  sr_ Jan 21 '13 at 9:51
thanks, its the bash shell causing problems –  munish Jan 21 '13 at 9:57
The correct way to write it would have been printf '#!%s' "$opt_E" BTW –  Stéphane Chazelas Jan 22 '13 at 21:47

2 Answers 2

up vote 3 down vote accepted

See Bash reference manual:


designates the last argument of the preceding command. This may be shortened to !$.

If this behavior is undesired you can just escape ! with backslash:

% echo "\!$"                  
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.