This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Closed 4 years ago.
I want to escape double quotes in Bash. I followed the following approach:
#!/bin/bash
this is a \"number\"!
But is there another way?
You can enclose the double quotes in single quotes:
echo '"'hola'"'
Or alternatively the whole text, including the double quotes:
echo '"hola"'
With GNU Bash:
echo -e "this is a \x22number\x22"
Output:
this is a "number"
Related
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
USER_UID=$1
echo 'generate_token("$USER_UID")'
I want output like
generate_token("1234567")
i tried multiple ways but didn't worked. it just print same line without value generate_jwt("$USER_UID")
When you use single quotes, it causes the shell to preserve the literal value of each character within the quotes. This means the $ will be treated as a literal $ character.
You should use double quotes:
USER_UID="$1"
echo "generate_token(\"$USER_UID\")"
From the bash man page, under the Quoting section:
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, !.
For POSIX details on quoting, see here.
Example in an interactive shell:
$ USER_UID='foo'
$ echo "generate_token(\"$USER_UID\")"
generate_token("foo")
This will also work if USER_UID contains spaces:
$ USER_UID='var with spaces'
$ echo "generate_token(\"$USER_UID\")"
generate_token("var with spaces")
This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Closed 5 years ago.
I want to add double quotes around my variable string, any advice on how I adapt my code? Simple example below
#!/bin/bash
variable="variable"
echo $variable
I'd like to see the output as
"variable"
Escape the quotes:
echo \"$variable\"
This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Closed 4 years ago.
I want to check whether or not the hidden .git folder exists. First thought was to use:
if [ -d "~/.git" ]; then
echo "Do stuff"
fi
But the -d apparently does not look for hidden folders.
The problem has to do with the tilde being within double quotes.
To get it expanded, you need to put the tilde outside the quotes:
if [ -d ~/".git" ]; then # note tilde outside double quotes!
echo "Do stuff"
fi
Or, alternatively, as commented below by hek2mgl, use $HOME instead of ~:
if [ -d "$HOME/.git" ]
From POSIX in Tilde expansion:
A "tilde-prefix" consists of an unquoted <tilde> character at the beginning of a word, followed by all of the characters preceding the first unquoted <slash> in the word, or all the characters in the word if there is no <slash>.
From POSIX in Double Quotes:
Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:
You can find further explanations in Why doesn't the tilde (~) expand inside double quotes? from the Unix & Linux Stack.
This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 8 years ago.
I want to search for a string enclosed in double quotes given by a positional parameter. This is what I've done:
#!/bin/bash
FOO=$0
sed -i 's/"${FOO}"/bar/m' file.txt
Neither "$FOO" nor \"a${FOO}\"/ work.
I am aware that for variables to be expanded, you have to enclose the whole regex in double quotes (sed substitution with bash variables). The thing is that the string I am searching for is also enclosed in double quotes, that is why I enclosed the whole regex in single quotes. I also tried to enclose it in double quotes and escaping the double quotes, but it didn't work.
An explanation of the logic behind single and double quotes escaping would be really useful.
Use double quotes to quote the sed commands as
sed -i "s/\"${FOO}\"/bar/m" file.txt
Test:
$ echo $b
hello
$ echo \"hello\" | sed "s/\"$b\"/asdf/"
asdf
This question already has answers here:
here document and double backslash
(2 answers)
Closed 8 years ago.
I am trying to pass some code to a REPL and this heredoc seems to be escaping my regex. To shorten the problem... I have a really long regex but this is the main problem:
<<SOMECODE
\\d
SOMECODE
This is returned by the heredoc as
\d
How do I get the heredoc to not remove my extra slash? I thought heredocs were immune to characters.
Quote Your Here-Document Delimiter
If you want to prevent most escapes and expansions, you can surround your Bash here-document delimiter with single quotes. For example:
cat << 'SOMECODE'
\\d
SOMECODE
prints \\d on my system.