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 user@example.com:"$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...

swap
  • 43
  • 1
  • 5

3 Answers3

4

You need to escape each spaces and brackets :

#!/bin/bash

aaa='/path/to/some\ file\ with\ spaces\(and brackets\).txt'
scp user@example.com:"$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 user@example.com:"'$aaa'" /test/
krisFR
  • 12,830
  • 3
  • 31
  • 40
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 user@example.com:"$aaa" .
Ryan Boyle
  • 106
  • 6
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
Zoredache
  • 128,755
  • 40
  • 271
  • 413