1

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}"
##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

munish
  • 7,987

2 Answers2

3

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 "\!$"                  
!$
gelraen
  • 6,737
2

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.