This question already has an answer here:
I need to run a command with a syntax like this:
runuser -l userNameHere -c '/path/to/command arg1 arg2'
Unfortunately, I have to nest additional '
characters into the command itself and I can't tell bash to interpret these correctly. The command I would like to run is actually:
runuser -l miner -c 'screen -S Mine -p 0 -X eval 'stuff "pwd"\015''
Unfortunately, bash seems to be hitting the second '
and puking. This is the error:
-bash: screen -S Mine -p 0 -X eval stuff: No such file or directory
, so obviously it's not getting past the '
.
How can I nest this as one command? Thank you!
See my answer here. Basically, you can't put single quotes inside single quotes - but you don't have to, because quotation marks aren't word delimiters, so you can step out of quotes, with a close
'
, add a literal single quote now that you're outside of the quotes with\'
, and then go back into quotes with a new open'
, all without terminating the current shell word.The command itself doesn't see the outer quotes. The shell uses those to avoid wordsplitting, but doesn't let the command know they were there. So if you have:
The command sees three args, foo, bar and baz biz with whitespace intact, but no quotes. Thus, if you need to actually send quotes, you can do that by wrapping the argument with the other kind of quote:
The command sees one arg: foo'bar. But if you need to send both kinds of quotes, you have a harder problem to solve. You can solve it with leaning toothpicks, quote-swapping or variables:
Quote swapping
Even though the shell uses quotes to avoid wordsplitting, if you put the quoted arguments next to each other without whitespace the command will see it as one word:
The command sees one arg: foobar. So if you use two different kinds of quotes:
The command sees one arg: foo"barbaz'quux.
Leaning toothpicks
There are two kinds of leaning toothpicks. One is really just quote swapping except you don't use quotes to wrap one of the …quotes.
The command sees one arg: foo"barbaz'quux. The other is (hat tip: chepner) to use the special
$'string'
form of word expansion, which allows ANSI C strings:The command sees one arg: foo"barbaz'quux.
Variables
The command sees one arg: foo"barbaz'quux.
You cannot nest single quotes within single quotes. You can nest double quotes within double quotes by escaping them though.
The best you can do with single quotes is to use
'\''
wherever you need an embedded single quote.That's a single quote to end the single quoted string. An unquoted, but escaped, single quote. And a single quote to start the next single quoted string.
You can use another type of quoting supported by
bash
,$'...'
. This can contain escaped single quotes.Note that within
$'...'
, the\015
will be treated replaced with the actual ASCII character at codepoint 015, so if that's not what you want, you'll need to escape the backslash as well.I think you can take advantage of the
$'...'
to remove the need foreval
as well:Instead of escaping things manually, you can ask the shell to do it for you and avoid the trouble (if your shell is
bash
; this is an extension not available in POSIX sh, so your shebang needs to be#!/bin/bash
, not#!/bin/sh
).For instance:
...will put a string in
"$ssh_command"
which, if evaluated, will run the inner command inside ofrunasuser
insidessh
.To provide some examples of how to use these variables, once constructed:
To provide some counterexamples of how NOT to use these variables, once constructed: