Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.