2

I have this bash script:

#!/bin/bash OriginFilePath="/home/lv2eof/.config/google-chrome/Profile 1/" OriginFileName="Bookmarks" OriginFilePathAndName="$OriginFilePath""$OriginFileName" DestinationFilePath="/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/" DestinationFileName=$(date +%Y%m%d-%H%M%S-Bookmarks) DestinationFilePathAndName="$DestinationFilePath""$DestinationFileName" echo cp \"$OriginFilePathAndName\" \"$DestinationFilePathAndName\" cp \"$OriginFilePathAndName\" \"$DestinationFilePathAndName\" 

When I execute it from the command line I get this output:

[~/] lv2eof@PERU $$$ csbp1 cp "/home/lv2eof/.config/google-chrome/Profile 1/Bookmarks" "/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/20211207-001444-Bookmarks" cp: target '1/20211207-001444-Bookmarks"' is not a directory [~/] lv2eof@PERU $$$ 

So I get an error and the file isn't copied. Nevertheless if I issue in the command line the command:

[~/] lv2eof@PERU $$$ cp "/home/lv2eof/.config/google-chrome/Profile 1/Bookmarks" "/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/20211207-001444-Bookmarks" [~/] lv2eof@PERU $$$ 

As you can see everything works fine and the file is copied. Shouldn't the commands work the same inside and outside bash scripts? What am I doing wrong?

5
  • Does this answer your question? cp doesn't work in script but works in terminal
    – vfbsilva
    CommentedDec 7, 2021 at 0:43
  • 3
    Why are you escaping the quotes in the script? That causes them to be interpreted literally instead of being used to account for the spaces. When accounting for the that, what is happening is rather evident.CommentedDec 7, 2021 at 0:52
  • It's because you escaped the double quote on the cp command. The destination path variable splits into two paths.
    – annahri
    CommentedDec 7, 2021 at 0:56
  • Similar to unix.stackexchange.com/questions/658837/… - using echo instead of -x.
    – muru
    CommentedDec 7, 2021 at 1:32
  • Note that the command that you typed in the terminal isn't the same as the one you typed in the script. Try cp \"/home/lv2eof/.config/google-chrome/Profile 1/Bookmarks\" \"/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile 1/20211207-001444-Bookmarks\" in the terminal and you'll get the same error.
    – Kusalananda
    CommentedDec 7, 2021 at 7:29

1 Answer 1

2

It is perhaps hard to notice, but message gives you two hints:

cp: target '1/20211207-001444-Bookmarks"' is not a directory | | | +-- Notice quote +-- Space in target 

In other words 1/20211207-001444-Bookmarks" is not a directory. So why is it saying that?

In your script you have:

cp \"$OriginFilePathAndName\" \"$DestinationFilePathAndName\" 

By escaping the quotes, you are saying the quotes are part of the arguments. Or: threat quotes as literal text. They are concatenated with the value of the variable.

Should be:

cp "$OriginFilePathAndName" "$DestinationFilePathAndName" 

In short: you quote the variable to tell bash this should be threaded as one argument.

From your question, the actual arguments to cp becomes 4, not 2:

  1. "/home/lv2eof/.config/google-chrome/Profile
  2. 1/Bookmarks"
  3. "/home/Config/Browser/Bookmarks/ScriptSaved/Chrome/Profile
  4. 1/20211207-001444-Bookmarks"

In other words copy 1, 2 and 3 into 4.

1
  • You are right. I tried your sugestion and it worked fine. The explenation for my mistake is simple: still newbie after all these years :)
    – Lv2eof
    CommentedDec 7, 2021 at 10:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.