0

rclone is a backup program I run from a bash script. I want to pass in this parameter to rclone:

--exclude "{secret1b,secret1c}/**"

rclone needs those quotes. Here is my failed attempt:

#!/bin/bash

cmd='rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**"'

printf "command: \n$cmd\n\n"
echo "$cmd"
#echo and printf output as expected:
#rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**"

#rclone with --exclude works as expected:
$(rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**")

#on these $cmd, rclone works but the --exclude is not excluding:
$cmd
$($cmd)

It's weird because the literal string output by echo "$cmd" works as expected.

$(rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**")

But the command $cmd does not work as expected.

$cmd

There are two similar questions that are NOT trying to pass in the quotes:

wolfv
  • 971
  • 12
  • 20
  • 2
    Don't put the command in a variable. See [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050) Oh, and the two questions you linked both give valid answers to this. – Gordon Davisson Feb 09 '18 at 05:56
  • did you try escaping the ". --exclude \"{secret1b,secret1c}/**\" – abhishek phukan Feb 09 '18 at 08:45
  • @abhishekphukan, making the quotes literal in that way completely changes the behavior from the original command (where they're syntactic). Moreover, the command that the OP is telling us works -- when they run the `echo` results as a shell command -- is a case where the quotes are syntactic (controlling how the shell divides argument lists) and not data (part of the individual `argv` elements passed to `rclone`). – Charles Duffy Feb 09 '18 at 19:47
  • @user2867994, you may **think** that you need to pass the quotes as literal data, but **YOU ARE WRONG**. If running the `echo "$cmd"` output works as expected when you copy-and-paste it back to a shell, then it's working with those quotes being parsed **as syntax**, not as data. – Charles Duffy Feb 09 '18 at 19:49
  • @user2867994, run `set -x` to tell the shell to log every command it runs, and compare the logs in that mode with the working command against those from your various attempts. – Charles Duffy Feb 09 '18 at 19:50
  • Alternately, if you want to display your working argument list **as data**, you can do the following: `printf '%s\n' rclone sync /home/wolfv/test_rclone_data/direc1 /home/wolfv/test_rclone_backup/last_snapshot/direc1 --exclude "{secret1b,secret1c}/**"` to emit each argument literally one-per-line. You'll see that the quotes aren't actually there in the literal argument list, because they're syntax, not literal shell data. – Charles Duffy Feb 09 '18 at 19:53
  • For the same reason, if you were writing your program in Python and using `subprocess.Popen(cmd, shell=False)`, then the Python array for correct operation would be `cmd=['rclone', 'sync', '/home/wolfv/test_rclone_data/direc1', '/home/wolfv/test_rclone_backup/last_snapshot/direc1', '--exclude', '{secret1b,secret1c}/**']` -- note again that the only quotes here are *syntactic* ones, instructions to the language (in this example Python); not *literal* ones passed to `rclone`. – Charles Duffy Feb 09 '18 at 19:55
  • If you read [the `rclone` documentation](https://rclone.org/docs/), you'll note that the instructions to use quotes are in a section called "Quoting and the shell", which tells you to refer to your shell's rules and documentation for details. That's for a reason -- the quotes are there *for the shell's use*, not for `rclone`'s. – Charles Duffy Feb 09 '18 at 19:58
  • @CharlesDuffy, Getting rid of the double quotes worked: --exclude {secret1b,secret1c}/** Thank you! If you post it as answer I will accept it, otherwise I will post the answer in 24 hours. – wolfv Feb 09 '18 at 21:29
  • @user2867994, that's really not a good answer, for all the reasons given in [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050). Please leverage the solutions given there, or in the questions you linked; [this answer by @chepner](https://stackoverflow.com/a/32586295/14122) is particularly on-point. – Charles Duffy Feb 09 '18 at 21:41

0 Answers0