4

as a part of script I'm trying to copy a file from remote site. But got an error. To me that sounds bit strange as everything sounds ok:

#aaa="/path/to/some file with spaces(and brackets).txt"
....
#scp [email protected]:"$aaa" /test/
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `scp -f /path/to/some file with spaces.txt'

upd: problem with brackets...

5
  • Please add the complete script
    – 030
    May 15, 2015 at 18:28
  • Put a set -x in your script before the problem so the exact command be executed is printed out on the screen?
    – Zoredache
    May 15, 2015 at 18:29
  • it doesn't really matters, at the moment i'm trying to debug that just from command line, and if you will just type in bash lines starting with "#" you will get the same error
    – swap
    May 15, 2015 at 18:35
  • You need to have the brackets escaped or quoted too, as bash wants to take action on them May 15, 2015 at 18:55
  • aaa="'/path/to/some file with spaces(and brackets).txt'" should escape it twice, once in single quotes, once in double quotes. May 15, 2015 at 19:11

3 Answers 3

4

You need to escape each spaces and brackets :

#!/bin/bash

aaa='/path/to/some\ file\ with\ spaces\(and brackets\).txt'
scp [email protected]:"$aaa" /test/

By the way, a more friendly alternative would be to enclose $aaa with single quotes in addition to double quotes :

#!/bin/bash

aaa='/path/to/some file with spaces(and brackets).txt'
scp [email protected]:"'$aaa'" /test/
2
  • echo $aaa |sed -r "s/(/\\(/g"|sed -r "s/)/\\)/g"|sed -r "s/ /\\\ /g"
    – swap
    May 15, 2015 at 19:06
  • @swap What do you mean with this sed command ?
    – krisFR
    May 15, 2015 at 19:47
1

Below worked for me. I think you just need to escape the spaces, brackets or anything else and you should be good.

#!/bin/bash

aaa="/tmp/untitled\ text\ 2.txt"

scp -r [email protected]:"$aaa" .
3
  • 1
    in that case you will copy local file to remote server
    – swap
    May 15, 2015 at 18:42
  • Yes, that is the reverse from question !
    – krisFR
    May 15, 2015 at 18:44
  • Updated answer above to get remote file.
    – Ryan Boyle
    May 15, 2015 at 18:46
0

I created a file on my remote host with the literal name `"/tmp/some file with spaces(and brackets).txt~.

If you double+single quote the name like so I was able to transfer it. Inspired by this question.

/tmp$ scp remotehost:"'/tmp/some file with spaces(and brackets).txt'" .
some file with spaces(and brackets).txt          100%    0     0.0KB/s   00:00

With a variable

/tmp$ aaa="/tmp/some file with spaces(and brackets).txt"
/tmp$ scp puppet:"'$aaa'" .
some file with spaces(and brackets).txt               100%    0     0.0KB/s   00:00
0

Not the answer you're looking for? Browse other questions tagged .