Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printf #!/bin/bash

28 views
Skip to first unread message

James

unread,
Apr 21, 2017, 2:19:40 PM4/21/17
to
Why do I get this error and how to fix it?

$ printf "#!/bin/bash\n\n" > script.sh
bash: !/bin/bash\n\n": event not found

TIA
James

Ed Morton

unread,
Apr 21, 2017, 2:33:39 PM4/21/17
to
The bash history expansion character `!` is enable in your shell, see
https://www.gnu.org/software/bash/manual/html_node/History-Interaction.html.

Remember the shell quoting rule is to use single quotes unless you NEED double
quotes and then use double quotes unless you NEED no quotes. You don't need
double quotes here so you shouldn't be using them and then your problems go away:

$ printf "#!/bin/bash\n\n"
-bash: !/bin/bash\n\n: event not found

$ printf '#!/bin/bash\n\n'
#!/bin/bash

Regards,

Ed.

Ivan Shmakov

unread,
Apr 22, 2017, 2:42:50 PM4/22/17
to
>>>>> Ed Morton <morto...@gmail.com> writes:
>>>>> On 4/21/2017 1:19 PM, James wrote:

>> Why do I get this error and how to fix it?

>> $ printf "#!/bin/bash\n\n" > script.sh
>> bash: !/bin/bash\n\n": event not found

> The bash history expansion character `!` is enable in your shell, see
> https://www.gnu.org/software/bash/manual/html_node/History-Interaction.html.

... And unless there's intent to actually use it, I'd suggest
disabling it altogether with 'set +H' (likely in ~/.bashrc.)

(FWIW, I use Readline's C-r function to search my history.)

> Remember the shell quoting rule is to use single quotes unless you
> NEED double quotes and then use double quotes unless you NEED no
> quotes. You don't need double quotes here so you shouldn't be using
> them and then your problems go away:

> $ printf "#!/bin/bash\n\n"
> -bash: !/bin/bash\n\n: event not found

> $ printf '#!/bin/bash\n\n'
> #!/bin/bash

I do not know the exact rules in play here, but I'm pretty sure
that some of my command lines featuring sed -e '/.../!d' got
that 'd' replaced with a prior 'df' command more than once --
despite of the use of single quotes. Hence I disabled it
altogether.

Never seen this issue occuring with a single \ escape (\!),
though.

--
FSF associate member #7257 ... And shopt -u hostcomplete also. B6A0 230E 334A
0 new messages