Double quotes are deleted from string passed as argument to sh in Jenkinsfile - string

Specifying
sh "bash -c \"echo Hello world\""
in a declarative pipeline in a Jenkinsfile results in
bash -c echo Hello world
being executed. I'd expect the escaping of the double quotes in the string to be resolved to double quotes like so
bash -c "echo Hello world"
Now, the quotes are simply deleted which is very unexpected if not buggy. I'd like to understand what's happening and eventually suggest and improvement to the Jenkins devs.
This might be another case of Jenkinsfile idiosynchrasies with escaping and quotes, however I don't seem to find the matching one.

Have you tried to play with the single quote:
sh "bash -c 'echo Hello world'"

Another option is
sh """bash -c "echo Hello world" """
This allows you to keep the double quotes for the cases you need interpolation inside your command.

Related

How do I run sed in TCL

I am trying to use TCLs builtin exec procedure to run the following sed shell command:
sed -i 's/"VALUE.${name}">.*</"VALUE.${name}">${value}</' ${dir}/imp.xml
However when I pass it to exec tcl errors out with
sed: -e expression #1, char 1: unknown command: `''
no idea how to interpret this.
I tried escaping the exec string:
exec {sed -i 's/"VALUE.${name}">.*</"VALUE.${name}">${value}</' ${dir}/imp.xml}
However this prevents the tcl variables from being expanded inside of the string.
Does anyone know what I need to do to get tcl to exec this sed program?
(my shell is csh if that is relevant)
The final solution involved 2 changes to the command string,
Escape all the double quote characters, (thanks #Chris Heithoff)
Single quotes are handled funny by TCL, replacing them with double quotes
(that are not escaped!) resolves the issue.
The final, working command string:
exec sed -i "s/\"VALUE.${name}\">.*</\"VALUE.${name}\">${alue}</" ${dir}/impl.xml
Each argument to the exec command must correspond to an individual argument at the shell command line, so enclosing everything in {} doesn't work.
Try this:, where all double quotes and dollar signs are escaped.
exec sed -i 's/\"VALUE.\${name}\">.*</\"VALUE.\${name}\">\${value}' \${dir}/impl.xml

Bash escape character

I have this very reduced example of a bash command, where I want the $ sign escaped.
So the command :
su -m user -c "echo $test"
should print out:
$test
a simple \$test does not work unfortunately. I tried lots of other stuff but still couldn't find a solution. Any suggestions ?
Put it in single quotes rather than double quotes.
su -m user -c 'echo \$test='
The single quotes keep the variable from being expanded by the original shell. The backslash then escapes the dollar sign in the shell run by su.
See Difference between single and double quotes in Bash
In answer to the comment, you can switch to double quoting to get single quotes into the string.
su -m user -c 'echo \$test='"'1'"

Print single quotes in shell script using option -c [duplicate]

This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 5 years ago.
This may sound novice but I tried everything to get this to work.
I want to print a word with single quotes : 'John' in shell script. I cant replace /bin/bash -l -c as this part of code is run by Java and I have to pass the shell command as a string. I tried with echo -e option as well.
/bin/bash -l -c 'echo "'John'"'
The output I want is:
'John'
I tried escaping the single quotes but nothing helped so far. Any ideas?
You can't nest single quotes in bash, so the line is interpreted as
/bin/bash -l -c 'echo "'John'"'
|......| ---------- single quoted
|....| ----- not quoted
|.| --- quoted
So, properly escape the single quotes:
/bin/bash -c 'echo "'\''John'\''"'
or, if the string in single quotes is really simple,
/bin/bash -c 'echo "'\'John\''"'
Try removing the double quotes and escaping the single quotes:
/bin/bash -l -c "echo \'John\'"
A common workaround is to use printf so you don't need to use literal single quotes in the format string.
bash -l -c 'printf "\x27John\x27\n"'
(Using bash -l here is probably misdirected and nonportable.)

Why does bash insert additional quotes

I need to pipe an expression including single quotes to a command, but bash inserts loads of extra quotes which breaks my command. As a really simple example take:
#!/bin/bash -x
echo 'EXPRESSION' | more
which gives:
+ echo EXPRESSION
+ more
EXPRESSION
As I want the single quotes to be displayed, I must escape them:
#!/bin/bash -x
echo \'EXPRESSION\' | more
Which now gives me:
+ echo ''\''EXPRESSION'\'''
+ more
'EXPRESSION'
So within the script, I get this bizarre ''\''EXPRESSION'\''' thing. The command I am piping the expression to is an executable that interacts with a document management system, and expects a specific format—which includes single quotes around EXPRESSION and not ''\'' and '\'''.
Is there any way to stop bash from adding the additional quotes and backslashes? I've messed around with strings and eval etc., but have failed to get rid of those additional quotes.
You can also try it with double quotes like this,
echo "'EXPRESSION'"|more
Output will be,
'EXPRESSION'
The /bin/bash -x is producing the top 2 lines. Your code produces the 3rd line. If you want you can just remove the -x and you should see it in a better way.
The above answer from Skynet works just fine, but with the -x option, it still shows 3 lines. It's just what the -x does.

Bash:Single Quotes and Double Quotes and Exclamation Mark

I have a simple script named example:
#!/bin/sh
echo $'${1}'
Please note that the usage of $'' here is to convert \n into new line.
${1} is the first parameter passed to this shell script.
I want to pass a parameter to this script example and it prints the following:
#1. You're smart!
#2. It's a difficult question!
I tried the following:
example "#1. You're smart!\n#2. It's a difficult question!"
An error: -bash: !\n#2.: event not found
Then I tried to escape ! by single quote, and tried:
example '#1. You're smart\!\n#2. It's a difficult question\!'
It outputs:
${1}
Any solution here? Thanks a lot!
$ cat t.sh
#! /bin/bash
echo -e $#
Or echo -e $1, or echo -e ${1} if you just want to process the first argument.
To get bash to stop trying to expand !, use set +H (see In bash, how do I escape an exclamation mark?)
$ set +H
$ ./t.sh "#1. You're smart!\n#2. It's a difficult question!"
#1. You're smart!
#2. It's a difficult question!
What's inside a $'' expression has to be a literal. You can't expand other variables inside it.
But you can do this:
echo "${1//\\n/$'\n'}"
Jan Hudec has an even better answer:
echo -e "$1"

Resources