Bash script double quote escaping - bash

I'm trying to run a command in a bash script using a variable as a parameter, but I am failing to correctly escape the quotes.
This is what I have have:
vsnames=$(comm -13 --nocheck-order $cur_ha_stat_report $old_ha_stat_report | awk -F\| '{print $1}' | tr '\r\n' ' ')
create event type=0xfff00033 text="$vsnames" mh=$dev_mh &
Where vsnames is a string with words separated by spaces.
When I run the script, this is what I get:
+ vsnames='VS1 VS2 '
+ create event type=0xfff00033 'text=VS1 VS2 ' mh=xxx
How to I put the $vsnames variable between quotes?
I have tried using \" but it didn't work, bash just added a bunch of unwanted single-quotes.
Any tips?
The command I actually need is:
create event type=0xfff00033 text="VS1 VS2 " mh=xxx

With your variable vsnames being set to the 8-character-string VS1 VS2 (as we can see from the trace), doing a
create event type=0xfff00033 text=\""$vsnames"\" mh=xxx
would set the argv[2] of the create command to
text="VS1 VS2 "
Explanation: I have enclosed $vsnames into single quotes, to tell bash that we don't want to do word splitting here. Then I wrapped this between literal \" quotes, because you want create to see these quotes.

Related

Passing string with single quotes from jenkins to file

In jenkins pipeline I'm defining a condition for where clause in sql statement:
else if (params.targetEnv == "dev"){
condition = "where DATABASENAME like '%dev%'"}
Then I want to replace the condition placeholder in sql file:
sh """sed -i 's/#condition#/${condition}/' ${sql_query_file}"""
But in the file I get where DATABASENAME like %dev% instead where DATABASENAME like '%dev%' that means without the single quotes.
Does anybody know how can I pass there the condition with the single quotes?
Solution
sh "sed -i \"s/#condition#/${condition}/\" ${sql_query_file}"
explanation
If you will use a sentence in which there is some variable, you should use double quotes. This is valid on jenkins and unix
def condition = "foo"
echo "$condition"
println "$condition"
sed sintax need single or double quotes
sed -i 's/SEARCH_REGEX/REPLACEMENT/g' INPUTFILE
sed -i "s/SEARCH_REGEX/REPLACEMENT/g" INPUTFILE
Double quote should be used if some var is used in the sed argument
You should escape the double quote inside of sed because in the parent string there are double quotes
echo " foo is \"bar\" "
Putting it all togeter
sh "sed -i \"s/#condition#/${condition}/\" ${sql_query_file}"

How to put single quotes in bash

ABcD!987zyz12388
I'd like to put single quotes around it to handle special character/s. This password is fetched and saved to a variable.
$mypass=`fetchpwd $sourceserver $loginacct`;
$mypass="'$mypass'";
print "My password is: $mypass\n";
The return looks like this
My Password is 'ABcD!987zyz12388
'
The quotes don't belong in the variable itself; they should be added by the printf statement that displays the value.
mypass=$(fetchpwd "$sourceserver" "$logginacct")
printf "My password is: '%s'\n" "$mypass"
If fetchpwd is adding an extra newline you can remove newlines using
mypass=$(fetchpwd "$sourceserver" "$logginacct" | tr -d '\n')

How to concatenate string variables that contain space? [duplicate]

How can I escape double quotes inside a double string in Bash?
For example, in my shell script
#!/bin/bash
dbload="load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"
I can't get the ENCLOSED BY '\"' with double quote to escape correctly. I can't use single quotes for my variable, because I want to use variable $dbtable.
Use a backslash:
echo "\"" # Prints one " character.
A simple example of escaping quotes in the shell:
$ echo 'abc'\''abc'
abc'abc
$ echo "abc"\""abc"
abc"abc
It's done by finishing an already-opened one ('), placing the escaped one (\'), and then opening another one (').
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('), placing a quote in another quote ("'"), and then opening another one (').
More examples: Escaping single-quotes within single-quoted strings
Keep in mind that you can avoid escaping by using ASCII codes of the characters you need to echo.
Example:
echo -e "This is \x22\x27\x22\x27\x22text\x22\x27\x22\x27\x22"
This is "'"'"text"'"'"
\x22 is the ASCII code (in hex) for double quotes and \x27 for single quotes. Similarly you can echo any character.
I suppose if we try to echo the above string with backslashes, we will need a messy two rows backslashed echo... :)
For variable assignment this is the equivalent:
a=$'This is \x22text\x22'
echo "$a"
# Output:
This is "text"
If the variable is already set by another program, you can still apply double/single quotes with sed or similar tools.
Example:
b="Just another text here"
echo "$b"
Just another text here
sed 's/text/"'\0'"/' <<<"$b" #\0 is a special sed operator
Just another "0" here #this is not what i wanted to be
sed 's/text/\x22\x27\0\x27\x22/' <<<"$b"
Just another "'text'" here #now we are talking. You would normally need a dozen of backslashes to achieve the same result in the normal way.
Bash allows you to place strings adjacently, and they'll just end up being glued together.
So this:
echo "Hello"', world!'
produces
Hello, world!
The trick is to alternate between single and double-quoted strings as required. Unfortunately, it quickly gets very messy. For example:
echo "I like to use" '"double quotes"' "sometimes"
produces
I like to use "double quotes" sometimes
In your example, I would do it something like this:
dbtable=example
dbload='load data local infile "'"'gfpoint.csv'"'" into '"table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '"'"'"' LINES "'TERMINATED BY "'"'\n'"'" IGNORE 1 LINES'
echo $dbload
which produces the following output:
load data local infile "'gfpoint.csv'" into table example FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "'\n'" IGNORE 1 LINES
It's difficult to see what's going on here, but I can annotate it using Unicode quotes. The following won't work in Bash – it's just for illustration:
dbload=‘load data local infile "’“'gfpoint.csv'”‘" into ’“table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '”‘"’“' LINES ”‘TERMINATED BY "’“'\n'”‘" IGNORE 1 LINES’
The quotes like “ ‘ ’ ” in the above will be interpreted by bash. The quotes like " ' will end up in the resulting variable.
If I give the same treatment to the earlier example, it looks like this:
echo “I like to use” ‘"double quotes"’ “sometimes”
Store the double quote character in a variable:
dqt='"'
echo "Double quotes ${dqt}X${dqt} inside a double quoted string"
Output:
Double quotes "X" inside a double quoted string
Check out printf...
#!/bin/bash
mystr="say \"hi\""
Without using printf
echo -e $mystr
Output: say "hi"
Using printf
echo -e $(printf '%q' $mystr)
Output: say \"hi\"
Make use of $"string".
In this example, it would be,
dbload=$"load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"
Note (from the man page):
A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.
For use with variables that might contain spaces in you Bash script, use triple quotes inside the main quote, e.g.:
[ "$(date -r """$touchfile""" +%Y%m%d)" -eq "$(date +%Y%m%d)" ]
Add "\" before double quote to escape it, instead of \
#! /bin/csh -f
set dbtable = balabala
set dbload = "load data local infile "\""'gfpoint.csv'"\"" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '"\""' LINES TERMINATED BY "\""'\n'"\"" IGNORE 1 LINES"
echo $dbload
# load data local infile "'gfpoint.csv'" into table balabala FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "''" IGNORE 1 LINES

Difference between single and double quotes in awk

I have this awk statement:
glb_library="my_library"
awk "
/^Direct Dependers of/ { next }
/^---/ { next }
/^$glb_library:/ { ver=\$0; next }
{ gsub(/[[:space:]]/, '', \$0); print ver':'\$0 }
" file
Basically, I have enclosed the awk code in double quotes so that the shell variable glb_library is expanded. I have made sure to escape the $ character to prevent the shell from expanding $0. Followed the guidance from here.
awk gives me this error:
awk: syntax error at source line 5
context is
{ gsub(/[[:space:]]/, >>> ' <<<
I want to understand:
Is it legal to use single quotes inside awk? Why is '' not a null string like "" is?
Does awk treat single and double quotes differently?
My code worked after I escaped the single quotes with backslashes and used \"\" to represent the null string instead of ''.
Never enclose any script in double quotes or you're sentencing yourself to backslash-hell. This is the syntax for what you're trying to do:
glb_library="my_library"
awk -v glb_library="$glb_library" '
/^Direct Dependers of/ { next }
/^---/ { next }
$0 ~ "^"glb_library":" { ver=$0; next }
{ gsub(/[[:space:]]/, ""); print ver":"$0 }
' file
Based on the comments above by awk experts and some research, I am posting this answer:
awk strings are enclosed in double quotes, not single quotes; more precisely: single quotes are not string delimiters in awk, unlike shell
awk attaches no special meaning to single quotes and they need to be enclosed in double quotes if used in string literals
it is best to use single quotes to wrap awk statements on command line, unlike OP's code that's using double quotes (Ed pointed this out clearly)
Further clarification:
"" is the null string in awk, not ''
to use single quotes in an awk string literal, enclose them in double quotes, as in "Ed's answers are great!"
other techniques followed while handling single quotes in awk are:
a) use a variable, as in awk -v q="'" '{ print q }' ...
b) use octal or hex notation, as in awk '{ print "\047"$0"\047" }' ...
Relevant documentation here.
A pragmatic summary:
As Ed Morton's helpful answer sensibly recommends:
Always use single quotes to enclose your awk script as a whole ('...'), which ensures that there's no confusion over what the shell interprets up front, and what awk ends up seeing.
To define strings inside an awk script, always use double quotes ("...").
" is the only string delimiter awk recognizes.
"..." strings are non-interpolating (you cannot embed variable references), but they do recognize control-character sequences such as \n and \t.
A single quote (') has no syntactic meaning inside an awk script, but, - if you're using '...' for your overall script, as recommended - you cannot use a literal ' inside of it anyway, because the shell's single-quoted strings do not permit embedded ' chars.
If you do need to use a literal single quote (') in your awk script, you have three choices:
Pass a variable that defines it, and use awk's string concatenation, based on directly adjoining string literals and variable references:
awk -v q=\' 'BEGIN { print "I" q "m good." }' # -> I'm good
Use an escape sequence inside "..."; for maximum portability and disambiguation, use an octal escape sequence (\047), not a hex one (\x27):
awk 'BEGIN { print "I\047m good." }' # -> I'm good
Use '\'' (sic) to "escape" embedded ' chars. (technically, 3 distinct single-quoted shell string literals are being concatenated)Thanks, snr:awk 'BEGIN { print "I'\''m good" }' # -> I'm good

How can I escape a double quote inside double quotes?

How can I escape double quotes inside a double string in Bash?
For example, in my shell script
#!/bin/bash
dbload="load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"
I can't get the ENCLOSED BY '\"' with double quote to escape correctly. I can't use single quotes for my variable, because I want to use variable $dbtable.
Use a backslash:
echo "\"" # Prints one " character.
A simple example of escaping quotes in the shell:
$ echo 'abc'\''abc'
abc'abc
$ echo "abc"\""abc"
abc"abc
It's done by finishing an already-opened one ('), placing the escaped one (\'), and then opening another one (').
Alternatively:
$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc
It's done by finishing already opened one ('), placing a quote in another quote ("'"), and then opening another one (').
More examples: Escaping single-quotes within single-quoted strings
Keep in mind that you can avoid escaping by using ASCII codes of the characters you need to echo.
Example:
echo -e "This is \x22\x27\x22\x27\x22text\x22\x27\x22\x27\x22"
This is "'"'"text"'"'"
\x22 is the ASCII code (in hex) for double quotes and \x27 for single quotes. Similarly you can echo any character.
I suppose if we try to echo the above string with backslashes, we will need a messy two rows backslashed echo... :)
For variable assignment this is the equivalent:
a=$'This is \x22text\x22'
echo "$a"
# Output:
This is "text"
If the variable is already set by another program, you can still apply double/single quotes with sed or similar tools.
Example:
b="Just another text here"
echo "$b"
Just another text here
sed 's/text/"'\0'"/' <<<"$b" #\0 is a special sed operator
Just another "0" here #this is not what i wanted to be
sed 's/text/\x22\x27\0\x27\x22/' <<<"$b"
Just another "'text'" here #now we are talking. You would normally need a dozen of backslashes to achieve the same result in the normal way.
Bash allows you to place strings adjacently, and they'll just end up being glued together.
So this:
echo "Hello"', world!'
produces
Hello, world!
The trick is to alternate between single and double-quoted strings as required. Unfortunately, it quickly gets very messy. For example:
echo "I like to use" '"double quotes"' "sometimes"
produces
I like to use "double quotes" sometimes
In your example, I would do it something like this:
dbtable=example
dbload='load data local infile "'"'gfpoint.csv'"'" into '"table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '"'"'"' LINES "'TERMINATED BY "'"'\n'"'" IGNORE 1 LINES'
echo $dbload
which produces the following output:
load data local infile "'gfpoint.csv'" into table example FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "'\n'" IGNORE 1 LINES
It's difficult to see what's going on here, but I can annotate it using Unicode quotes. The following won't work in Bash – it's just for illustration:
dbload=‘load data local infile "’“'gfpoint.csv'”‘" into ’“table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '”‘"’“' LINES ”‘TERMINATED BY "’“'\n'”‘" IGNORE 1 LINES’
The quotes like “ ‘ ’ ” in the above will be interpreted by bash. The quotes like " ' will end up in the resulting variable.
If I give the same treatment to the earlier example, it looks like this:
echo “I like to use” ‘"double quotes"’ “sometimes”
Store the double quote character in a variable:
dqt='"'
echo "Double quotes ${dqt}X${dqt} inside a double quoted string"
Output:
Double quotes "X" inside a double quoted string
Check out printf...
#!/bin/bash
mystr="say \"hi\""
Without using printf
echo -e $mystr
Output: say "hi"
Using printf
echo -e $(printf '%q' $mystr)
Output: say \"hi\"
Make use of $"string".
In this example, it would be,
dbload=$"load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"
Note (from the man page):
A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.
For use with variables that might contain spaces in you Bash script, use triple quotes inside the main quote, e.g.:
[ "$(date -r """$touchfile""" +%Y%m%d)" -eq "$(date +%Y%m%d)" ]
Add "\" before double quote to escape it, instead of \
#! /bin/csh -f
set dbtable = balabala
set dbload = "load data local infile "\""'gfpoint.csv'"\"" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '"\""' LINES TERMINATED BY "\""'\n'"\"" IGNORE 1 LINES"
echo $dbload
# load data local infile "'gfpoint.csv'" into table balabala FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "''" IGNORE 1 LINES

Resources