Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The command executed in the following script works:

#!/bin/bash -xv
REM_DIR="/home/jan/rem-dir"
SSHOPTS="-C -c arcfour256 -o "\""ServerAliveInterval 15"\"
SSHUSER="jan@example.com"

CMD="ssh $SSHOPTS \"$SSHUSER\" \"find \\\"$REM_DIR/\\\" -path *lost+found -prune -o -type f -printf \\\"%P\\\\n\\\"\""
REM_FILES=`eval $CMD`

It basicaly executes this on the remote host:

find "/home/jan/rem-dir/" -path *lost+found -prune -o -type f -printf "%P\n"

But how should it be escaped/formatted if I want it this way:

REM_FILES=`ssh ...`

Or:

REM_FILES=$(ssh ...)

Though my current script run just fine, I would like to know how to properly escape/quote when shell scripting.

share|improve this question
    
I think youll get more support for this type of question here unix.stackexchange.com – Rhyuk Jul 13 '12 at 14:46
    
That sort of question is entirely on topic on SU as well – Journeyman Geek Jul 13 '12 at 15:19
    
Please see BashFAQ/050 in other words, don't use all those variables. – Dennis Williamson Jul 14 '12 at 0:50

A better way is:

#!/bin/bash -xv
REM_DIR="/home/jan/rem-dir"
SSHOPTS=(-C -c arcfour256 -o "ServerAliveInterval 15")
SSHUSER="jan@example.com"

REM_FILES=`ssh "${SSHOPTS[@]}" "$SSHUSER" "find \"$REM_DIR/\" -path \"*lost+found\" -prune -o -type f -printf \"%P\\n\""`

The main problem was with $SSHOPTS, the trick is to put each argument in a separate element of an array.

@Dennis thanks for the useful link.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .