Understand double quote with echo and shebang [duplicate] - shell

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
! in a string makes it unusable in a command line - error message "event not found" [duplicate]
(1 answer)
-bash: !/usr/bin/env: event not found [duplicate]
(1 answer)
Closed 3 years ago.
I have a basic question about Shell interpretation
When i make something like :
echo "#!/bin/bash"
-bash: !/bin/bash: event not found
What does the shell really pass to the echo command.
If i understand well, the double quote make the shell doesnt do globbing.

! is used for history expansion, not for globbing.
From the bash documentation:
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !.

Related

What does putting quotes around $# in bash script do? i.e. "$#" instead of $# [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 months ago.
I was getting errors passing in a command and when I put quotes around that things worked just fine. Just curious how that works.
Double quotes around $# (and similarly, ${array[#]}) prevents globbing and word splitting of individual elements, while still expanding to multiple separate arguments.
See: https://github.com/koalaman/shellcheck/wiki/SC2068

How to add quoted parameters to a quoted command in a shell script? [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Closed 2 years ago.
I want to compose a command in a shell script like this:
#!/bin/sh
APPLICATION="date"
PARAMETER="-d '2020-01-01 1:23'"
CMD="${APPLICATION} ${PARAMETER}"
${CMD}
The 'PARAMETER' is supposed to hold parameters that need to be quoted themself. Unfortunately it does not work like this. Escaping them via PARAMETER="-d \"2020-01-01 1:23\"" also does not work.
After you've build CMD up, it is just string. It contains what can be interpreted by you as a command, but the shell sees it as a bare string.
If you want the string to reinterpret it, you need to eval it:
eval "$CMD"
However, eval is often considered evil.

In bash, what does dir=${0%/*} means? [duplicate]

This question already has answers here:
What does 'cd ${0%/*}' mean in bash?
(2 answers)
What is the meaning of ${0%/*} in a bash script?
(1 answer)
Closed 6 years ago.
I have found this piece of code while studying a bash script:
dir=${0%/*}
I suspect the code inside the braces to be regex but I don't see what it means. Any idea?
It is not a regex, but it is a pattern match. It sets dir to the name of the script, which is $0, but without the last slash and any non-slash after it, if there is a slash in $0. If there is no slash in $0, dir gets a copy of $0 unchanged. See "Parameter Expansion" in the Bash Hackers Wiki.

Bash variable expansion in command [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
DATE="1 week ago"
date --date='$DATE'
doesn't work. How can I get it to work?
I could do:
DATE_CMD="date --date='$DATE'"
eval $DATE_CMD
but I don't want to store the entire command in a variable.
You're a victim of quote expansion.
The proper invocation would likely be:
DATE='1 week ago'
date --date="$DATE"
(notice the double quotes)
You just need to use double-quotes to enable string interpolation:
date --date="$date"

"Event not found" error for shell command in unix [duplicate]

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
Closed 5 years ago.
when i am trying to remove consecutive duplicate lines with
awk "!x[$0]++" file
its reporting x[: Event not found.
even the same case with
sed -i -e "$!N; /^\(.*\)\n\1$/!P;D" file as well reporting
N: Event not found.
i tried with single quotes too, it didn't help
Any idea to fix those
You're invoking the shell's history substitution. Surround the exclamation point with single quotes.

Resources