Shell Quoting Conundrum

Somebody on a mailing list asked a question about shell quoting. The quoting issue would not have been so difficult had it not been for the fact that it was a command that was to be run on the other end of an SSH connection. Here’s the command he was trying to run:

ssh -t hostname ‘sudo sh -c “echo \’test\’ ; echo \’test\’” ‘

My understanding is that the person running this wants to see output from this command that looks like this:


'test'
'test'

Here’s how I believe the command would need to look to get that output:

ssh -t hostname “sudo sh -c \”echo \’test\’; echo \’test\’\”"

The key is starting with double quotes instead of single quotes, because single quotes fed to a bash shell is a toggle switch that turns shell expansion on and off. Until about 5 minutes ago, I had always had the rule “Don’t put single quotes inside single quotes” in my brain as a rule of thumb to avoid confusion. After doing some checking (in the bash man page), turns out it’s a bona fide RULE in bash. From the man page:

A single quote may not occur between single quotes, even when preceded by a backslash.

Whaddya know? I must’ve picked that up along the way and forgot about it.

If you think about the single quote as a toggle between ‘expansion on’ and ‘expansion off’, and NOT as a literal single quote, and then parse that first command again, you’ll see that this is probably not going to work.

Technorati Tags: , , , , , ,

Social Bookmarks:

  • http://steve-parker.org/ unixshell

    You’re right, though. Going from a Solaris 10 (SPARC) box to an x86 Ubuntu box:

    bash-3.00$ ssh -t elvis “sudo sh -c \”echo \’test\’; echo \’test\’\”"
    steve@elvis’s password:
    ‘test’
    ‘test’
    Connection to elvis closed.
    bash-3.00$