-2
#!/bin/bash

documents=("/datalog/AB errors.txt");

/usr/bin/bash /home/user/download.sh "${documents[*]}"

the download.sh SCPs into a server and downloads documents but in the case above there is a space in the file name which leads it to seek /datalog/AB instead of AB errors.txt file.

I thought surrounding in double quotes fixes the space in filename issue. I also tried AB\ errors.txt but that caused the entire bash script to not run.

abbb
  • 11
  • 4
  • 5
    `"${documents[*]}"` != `"${documents[@]}"`. Try changing `*` (a string) to `@` (an array) first then let us know if you still have a problem. – Ed Morton Feb 13 '23 at 22:32
  • 1
    That's the difference between `[*]` and `[@]`. `*` performs word splitting after substituting the values, `@` substitutes each value as a single word. – Barmar Feb 13 '23 at 22:43
  • 2
    It also depends on how download.sh handles its parameters. – choroba Feb 13 '23 at 22:46
  • hi @EdMorton, still having issues by replacing * to @ – abbb Feb 13 '23 at 23:00
  • 3
    the same issues or different issues? Make sure to [edit] your question to show your current code and describe your current problem. It sounds like `download.sh` may contain bugs so you'd have to show us a [mcve] including the relevant code from that script for us to be able to help you debug it. – Ed Morton Feb 13 '23 at 23:09
  • 1
    Spaces in strings need to be protected at every step in their handling; double-quotes here protect them as they're passed to `download.sh`, but do nothing to protect them after that. `download.sh` may be mishandling them, or it may be a problem with `scp` passing them to a remote shell without proper protection. See my answer [here](https://stackoverflow.com/questions/9483171/escaping-special-characters-in-bash-variables) for an example of protecting characters in the remote filename for `scp` (though you'd have to add space to the list of protected characters). – Gordon Davisson Feb 13 '23 at 23:29
  • @GordonDavisson thanks, in download.sh it is accessing the document string via `documents=${1}`, I'm guessing this is where I need to add the protection? – abbb Feb 14 '23 at 01:14
  • @abbb A plain assignment like that is one of the (very few) places where double-quotes can be safely omitted (though adding them wouldn't hurt). I'd recommend running the script through [shellcheck.net](https://www.shellcheck.net) and fixing anything it points, as it's good at spotting common problems like missing double-quotes. If it still has trouble after that, it's probably related to how the remote filename is handled. – Gordon Davisson Feb 14 '23 at 01:29

1 Answers1

0

The form

documents=("/datalog/AB errors.txt")

is bad form (turning string into an array !!!). The correct form for assignment is

documents="/datalog/AB errors.txt"

Always assume full-path variables can have spaces or other characters, and always double-quote such variable instantiations, i.e.

ls "${variable}" .

As Gordon Davisson said, you must code in this fashion at every level of scripting/programming to ensure the embedded special characters don't lead to unexpected results.

Eric Marceau
  • 1,601
  • 1
  • 8
  • 11