Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am trying to write a function that I will put on all my machines in order to make it easy to send files at a fixed place on my network.

Here is my script so far. Some folder may have duplicates names on my machines, so I'm adding a uuid at the end of the folder name.

function putOnSG3() {
if [[ -d $1 ]]; then scp -rv "$1" shiny:/Volumes/Seagate3To/"$1.$uuid";
else echo $1 " is not a directory. Not copying.";

I'm invoking it like this:

$ putOnSG testFo\[l\}der

Here is the problem:

zsh:1: bad pattern: /Volumes/Seagate3To/testFo[l}der.d84abc26-501b-4f89-a636-518b4059a770

How can I manage these nasty filenames ?

Target filesystem is case sensitive hfsplus, source filesystems are various extfs from Linux machines and NTFS.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Here is a working solution:

function putOnSG() {
if [[ -d $1 ]]; then du -sh "$1";
scp -rv "$1" shiny:/Volumes/Seagate3To/\"$1\".$uuid
else echo $1 "is not a directory. Not copying.";

The only differences with my initial non working solution are the backslashes around the quotes in the second argument of scp.

share|improve this answer
You've stumbled on the correct solution. The special consideration with scp arguments is they have to be "double-quoted". This is because an scp argument, like any normal shell argument, is interpreted by your shell. However, once scp gets the argument, it has to pass it to a remote shell on the other machine, and the shell there interprets it once again. That is why two layers of quoting are required (\ and "). –  jw013 Dec 14 '12 at 15:37
I asked a question specifically about scp and quotes: if you want to elaborate on the subject of double-quoting which is still not very clear for me. –  Antoine Lecaille Dec 14 '12 at 15:44
What part is unclear? I'm not sure I could explain it any better than what I said in the above comment. Maybe someone else can explain it better. –  jw013 Dec 14 '12 at 15:57
The part where you say that the remote shell interprets the argument once again. Any example involving uname ? –  Antoine Lecaille Dec 14 '12 at 16:02
The idea is any argument to scp goes through two shells - first the local one and then remote one. Since shells strip quotes, if you need the remote shell to see quotes, you'll need to quote them so the local shell doesn't remove them. Not sure how uname is relevant? –  jw013 Dec 14 '12 at 16:11

All special symbols, like [,],(,),#, etc you should write after backslash "\". You can try do it:

$ touch test\[ ; ll test\[ ;
-rw-r--r-- 1 s.gvozdetskiy s.gvozdetskiy 0 Dec 12 12:00 test[
$ mkdir test\[ ; ll ./
drwxr-xr-x 2 s.gvozdetskiy s.gvozdetskiy     4096 Dec 12 12:04 test[

I think hfsplus support there specific.

share|improve this answer
The problem occurs when scp receives the already quoted argument. The shell already takes care of the quoting (with tab completion for example) in the case I test the function manually. –  Antoine Lecaille Dec 12 '12 at 8:34
So what's problem? Copy successful: scp -rv test\[/ s.gvozdetskiy@foo-server:/home/s.gvozdetskiy s.gvozdetskiy@'s password: [s.gvozdetskiy@foo-server ~]$ ll ... drwxr-xr-x 2 s.gvozdetskiy s.gvozdetskiy 4096 Dec 12 12:41 test[ ... Or youre problem in function? –  Sergey Gvozdetskiy Dec 12 '12 at 8:45
Is your scp inside a function? –  Antoine Lecaille Dec 12 '12 at 9:07
No, from console and no problem. I modify your script and copy successfull too: $ cat #!/bin/sh function putOnSG3() { uuid=uuidgen` if [[ -d $1 ]]; then scp -i /s.gvozdetskiy/.ssh/id_rsa -rv "$1" s.gvozdetskiy@foo-server:/home/s.gvozdetskiy/"$1.$uuid" &2>1; else echo $1 " is not a directory. Not copying."; fi } putOnSG3 test[` I used ssh-keys for authorize on foo-server. –  Sergey Gvozdetskiy Dec 12 '12 at 9:54

Your Answer


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.