sakal
sakal

Reputation: 275

cp from string as source destination

I have a shell variable $allpath with the value

"/opt/in1/InputFile1.csv  /opt/InputFile2.csv" 

means , when I do echo, I get the above string.

Now I try to do the following:

cp "$allpath" /opt/targetdir    

and I get error:

cp: cannot stat ‘ /opt/in1/InputFile1.csv /opt/in2/InputFile2.csv’: No such file or directory

but when I manually type in

cp /opt/in1/InputFile1.csv  /opt/InputFile2.csv  /opt/targetdir    

it does the copy so how can I do it with the shell variable?

Upvotes: 1

Views: 517

Answers (2)

Mureinik
Mureinik

Reputation: 311723

The quotes are making the shell interpret it as one file, which obviously doesn't exist. Drop them, and you should be fine:

cp $allpath /opt/targetdir

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798884

In shells that support arrays, use them.

allpath=("/opt/in1/InputFile1.csv" "/opt/InputFile2.csv")
cp "${allpath[@]}" /opt/targetdir

Upvotes: 0

Related Questions