Find the answer to your Linux question:
Results 1 to 3 of 3
I have a generic script that builds up the parameters to call rsync. I am trying to add an optional set of parameters and pass this into a function that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    2

    Bash - Variable with spaces and quotes passed to function - help!


    I have a generic script that builds up the parameters to call rsync. I am trying to add an optional set of parameters and pass this into a function that generates and executes the rsync command. The extra parameters I want to pass is --rsh="ssh -p 2000".

    So in my caller I have something along the lines of
    EXTRAPARAM="--rsh=\'ssh -p 2000'"

    Then I call the function
    testFunc $PARAM

    Running this shows (below) and seems to drop the arguments when it see the space.
    ++ ARG='-n --rsh=\'\''ssh'

    If I call testFunc "$PARAM" to keep the whole variable contents I get
    ++ ARG='-n --rsh=\'\''ssh -p 2020\'\'''


    As a test (not the real scripts) I wrote up the following to try various scenarios. Any one point me to the solution (if any?!)

    set -x

    testFunc() {

    local ARG1=$1
    ARG="-n"
    ARG="$ARG $ARG1"

    rsync $ARG
    }
    PARAM="--rsh=\'ssh -p 2000\'"

    testFunc $PARAM
    testFunc "$PARAM"

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    How about a combination of escaping the double-quotes, and evaling it?, e.g.:
    Code:
    #!/bin/bash
    
    testFunc() {
      opts="--rsh=\"ssh -p 22\""
    }
    
    testFunc
    
    eval rsync $opts -av foo.txt remote_host:/tmp/

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    2
    Think that works actually!
    Last edited by ak888; 08-22-2011 at 03:42 PM. Reason: Thought it didn't work at first...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •