Musings of an anonymous geek

February 13, 2007

Shell Quoting Conundrum

Filed under: Scripting,Sysadmin,Technology — m0j0 @ 10:38 pm

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:

Advertisements

1 Comment »

  1. 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$

    Comment by unixshell — March 5, 2007 @ 6:46 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: