Rclone command string parse error in shell script

I am trying to write a shell script containing a rclone move command to backup my files to my drive regularly, but since I have a folder whose name has space in between, the parsing of the string goes unexpectedly.

These lines are in my shell script:

rcloneCommand="rclone move /Users/me/Archived/Screenshots/. myGDrive:MacBook\ Backups/Screenshots/ -vv"
echo $rcloneCommand
$rcloneCommand

When I run it, these are what I got from the shell:

./rcloneScreenshots
rclone move /Users/me/Archived/Screenshots/. myGDrive:MacBook\ Backups/Screenshots/ 
2018/04/24 00:26:36 DEBUG : rclone: Version "v1.40" starting with parameters ["rclone" "move" "/Users/me/Archived/Screenshots/." "myGDrive:MacBook\\" "Backups/Screenshots/" "-vv"]
    Usage:
      rclone move source:path dest:path [flags]
    ...
    Command move needs 2 arguments maximum

Apparently, the problem is because I have a folder name “MacBook Backups” in myGDrive, and if you take a look at the DEBUG array, the folder name gets cut here with adding an extra slash: "myGDrive:MacBook\\" "Backups/Screenshots/".

Knowing that to add a space for a folder name in a shell script is usually by adding a slash before the space, then how I could make this script work correctly? I used to use the string value in $rcloneCommand directly as a shell command to use rclone move and it runs without a problem.

I am not so sure if this is the right place to ask this question since it is not technically related to the features of rclone, but I guess it is sort of related to its parsing mechanism? Please advise. Thanks!! :grinning:

I recommend you don’t do this

As the escaping in the first line is executed then and there is no escaping later.

If you want to see what your shell script is doing using use set -v.

instead of doing that.

So I’d write this instead

#!/bin/bash
set -v
rclone move /Users/me/Archived/Screenshots/. myGDrive:MacBook\ Backups/Screenshots/ -vv

Also rather than \ quoting the spaces, it is easier to put them in “” I find, so

#!/bin/bash
set -v
rclone move /Users/me/Archived/Screenshots/. "myGDrive:MacBook Backups/Screenshots/" -vv

Hope that helps!

1 Like

Thank you very much ncw you saved my day again! :smiley:

These two ways do work(I tried both of them)! Actually, at first I did use the first way you introduced to write my script, but then I was trying to make my script more versatile since in myGDrive, except “MacBook Backups”, I have a bunch of other folders whose names have space as well, so I named a variable in my script like

gDriveFolder="MacBook\ Backups/"

and then construct my command string like

rcloneCommand="rclone move /Users/me/Archived/Screenshots/. "$gDriveFolder"Screenshots/ -vv"

So if the next time I want to change my backup folder in gDrive, I can just change the value of gDriveFolder (In fact in my script Screenshots is also a folderName variable and it may contain space too). The main reason is I am planning to write an if-statement in the future to back up certain folder based on my choice.

I then tried to combine your two ways with the use of the variables, but I couldn’t find a way to get around it. Hence, based on

then it would be difficult to introduce a variable whose value contains space into the command string?

Thanks!!

Assuming you’ve set a variable

gDriveFolder="MacBook\ Backups/"

You use this like this

rclone whatever drive:"$gDriveFolder"/more/path

Which will make sure the spaces are escaped.

(Just don’t put " in your variable!)

Finally this thing works! I should learn more about the scripting syntax!

Thanks! Appreciate it! :blush:

:smiley:

Cut and paste from stack overflow works pretty well to a certain level! Beyond that read the man page . However I prefer not to write really complicated shell scripts, I reach for a proper programming language like python or go (usually as soon as I start writing loops or functions!).