Eric
Eric

Reputation: 43

Bash Script single quotes inside of double quotes inside of single quotes

Yeah, this is kind of messed up but I have to do something like the following (this is just a simplified example):

sudo -H -u user1 bash -c ‘ssh -I /home/user1/.ssh/id_rsa -f [email protected] “find ./ -name ‘*.txt’”’

I have a need to have single quotes which are inside of double quotes which are inside of single quotes. Doesn't seem the standard '"'"' would work since the doubles would close the doubles rather than quoting a single. Would it maybe be: "'"'""' for each single I need inside there?

I've tried just skipping the ' around the ssh command but of course that failed.

Upvotes: 0

Views: 1268

Answers (2)

maximus
maximus

Reputation: 72

chepner's answer is sufficient. If bash -c required, this solution which looks messy can be used:

sudo -H -u user1 bash -c 'ssh -I /home/user1/.ssh/id_rsa -f [email protected] "find ./ -name '"'"'*.txt'"'"'"'

explanation

'... "find ./ -name '[first ' ended here]  "'" [second ' quote start]  '*.txt'[pattern]  "'"[second ' end] '"'[ " ended here]

I replaced with '.

Upvotes: 0

chepner
chepner

Reputation: 531055

You don't need the bash -c layer; just run ssh itself directly from sudo.

sudo -H -u user1 ssh -I /home/user1/.ssh/id_rsa -f [email protected] "find ./ -name \"*.txt\""

(The -I option should be unnecessary; if ssh is run as user1, then the default key is ~user1/.ssh/id_rsa. You may have meant -i anyway; -I specifies a PKCS#11 library to use, not a private key.)

That said, you cannot include single quotes inside a single-quoted string. When in doubt, use double quotes, and properly escape any nested double quotes.

Upvotes: 2

Related Questions