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

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.)

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'"

How to define $i in linux bash shell? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I am having a problem while using the bash shell. Here is my linux command code:
for i in `cat linshi`;do sed -i '/$i/d' a.txt;done
The content of linshi is:
aa
bb
The content of a.txt is:
aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg
I want to delete the first and the second row of a.txt.
But unlucky, I found '/$i/d' is not correct. And I have tried '/\$i/d' and '/"\"$id/', but they are fail again. Who can help me?
Variables aren't expanded inside single quotes, only double quotes.
for i in `cat linshi`; do sed -i "/$i/d" a.txt; done
That said, you could do the same thing with:
grep -vf linshi a.txt
Instead of using single quotes use double quotes. '' doesn't undergo any variable expansion however double quotes do.
This will work:
for i in $(cat linshi);do sed -i "/$i/d" a.txt;done

Escape quotes inside su -c

I would like to pass a path potentially containing whitespace into an su -c command inside a .sh script:
su -c "xdg-desktop-menu install ${DesktopFile}" -m "${Username}"
$DesktopFile is my path that needs to get escaped. I tried many variations with ' inside "", with \" and several braces, but I have not come up with a solution. Interestingly enough, xdg-desktop-icon works fine, but xdg-desktop-menu dies at the whitespace. Any suggestions?
If you're using bash, printf '%q' can be used to escape special characters when quoting is insufficient.
su -c "xdg-desktop-menu install $(printf '%q' "$DesktopFile") -m $(printf '%q' "$Username")"
Notice that $(printf...) doesn't need quotes, but "$DesktopFile" does. The variable needs to be passed to printf safely, so it has to be quoted. printf '%q''s output is guaranteed to be immune to word splitting and globbing, so it doesn't need quotes. (Although, if you wanted to quote it, you could. It would need \" on either side.)
i can't make out if this is a bug in xdg-desktop-menu, since it works in xdg-desktop-icon, nevertheless there is a workaround I am now using:
cd "$PathWithSpaces"
su -c "xdg-desktop-menu install $DesktopFilenameWithoutSpaces" -m "$Username"
PathWithSpaces contains DesktopFilenameWithoutSpaces, seems to work reliably.

Passing quoted variables in remote ssh command

I want to grep through a few files for a string. In this example I want to grep for "test 1234"
#!/bin/bash
variable="test 1234"
ssh root#server "grep "$variable" /path/*"
This script doesn't work because test 1234 is passed to the server instead of "test 1234"
How can I fix this?
You need to escape the quotes around $variable in your command:
ssh root#server "grep \"$variable\" /path/*"
As it stands, the variable is expanding but because the quotes aren't escaped, the command is searching for the text test in the files 1234 and /path/*
You should be able to escape the double quotes with a \ backslash character.
see escape double quote in grep

Resources