Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping quotes when using SSH

I'm trying to build a simple deployment script for my PHP apps. I know there are several tools for this job (Capistrano, Phing, etc.) but they seem like a lot of work for my simple deployment routine.

I use sshpass to avoid typing my password over and over again. But after uploading my compressed installer, I need to ssh into the server and run some commands. One of which is sed. So, quotes are breaking my script. It's something like this:

sshpass -p foo ssh user@host "
   cd /www/htdocs/foo/bar 
   echo 'Untar and remove installer'
   tar -zxf install.tar.gz

   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');" index.php > tmp && mv tmp index.php
   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');/" admin/index.php > tmp && mv tmp admin/index.php

"

As you can see, I use double-quotes to start my SSH statements, but I also need to use them on sed.

Any suggestions would be greatly appreciated. Thanks!

like image 703
fedeisas Avatar asked Jul 08 '11 08:07

fedeisas


People also ask

How do you escape a quote in Shell?

Escape with a backslash every double quote character and every backslash character: " ==> \", \ ==> \\

How do you escape characters quote?

To print quotes, using escape characters we have two options: For single quotes: \' (backslash followed by single quote) For double quotes: \” (backslash followed by double quotes)

How do I escape a single quote in Linux?

A single quote is not used where there is already a quoted string. So you can overcome this issue by using a backslash following the single quote. Here the backslash and a quote are used in the “don't” word.

How do you escape a single quote from a string?

You need to escape a single quote when the literal is enclosed in a single code using the backslash(\) or need to escape double quotes when the literal is enclosed in a double code using a backslash(\).

How do you escape a double quote in Linux?

' appearing in double quotes is escaped using a backslash. The backslash preceding the ' !


2 Answers

Escaping the internal quote marks is the normal way. Does this not work?

sshpass -p foo ssh user@host "
cd /www/htdocs/foo/bar
echo 'Untar and remove installer'
tar -zxf install.tar.gz

sed \"s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');\" index.php > tmp && mv tmp index.php
sed \"s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');/\" admin/index.php > tmp && mv tmp admin/index.php

"
like image 107
Flimzy Avatar answered Oct 14 '22 17:10

Flimzy


Can a here-document be used instead?:

sshpass -p foo ssh user@host <<DATA
   cd /www/htdocs/foo/bar 
   echo 'Untar and remove installer'
   tar -zxf install.tar.gz

   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');" index.php > tmp && mv tmp index.php
   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');/" admin/index.php > tmp && mv tmp admin/index.php
DATA
like image 40
wallyk Avatar answered Oct 14 '22 18:10

wallyk