0

I have a variable with the below content

 echo $sourcePath
 "/My Data Folder"

I am passing this to a command which has a switch called --path

I tried passing this as

--path $sourcePath 

But the command does not seem to like it.

If I do this manually, it works

--path "/My Data Folder"

I tried enclosing these in " and ' but no luck there. How can I ensure the input to the --path switch is exact data contained in my $sourcePath variable including the double quotes? Thanks

Suhas
  • 17
  • 4
  • 1
    `sourcePath='/My Data Folder'; --path "$sourcePath"` If that still has issues then try to debug. – Jetchisel Nov 05 '21 at 20:02
  • 1
    You're not supposed to have double quotes _inside your variable's content_ unless those quotes are actually part of the folder's name. Those quotes should only be present _as shell syntax_, not as data. Jetchisel's example shows how to do it correctly. – Charles Duffy Nov 05 '21 at 20:09

1 Answers1

-1

In which operative system are you testing this script? Why do you need double quotes? If it's a path, you don't need it in Linux. For Windows you can try using backslash for scaping the spaces in the folder name:

sourcePath="/My\ Data\ Folder"
Rafa Moyano
  • 169
  • 2
  • 8
  • It's ubuntu. If I do `--path /My\ Data\ Folder` it works well, However, the same value when passed to `--path` from a variable does not give what is expected. And the variable content now is `echo $sourcePath /My\ Data\ Folder` – Suhas Nov 05 '21 at 13:36
  • I was able to get this done by `"${sourcePath[@]}"` – Suhas Nov 05 '21 at 14:42
  • @RafaMoyano this example is incorrect. The backslashes are not necessary or appropriate inside a quoted string; `sourcePath="/My Data Folder"` works as-is, as long as one uses `--path "$sourcePath"` later. And if one _does not_ quote on expansion, then adding literal quotes in the data does not do any good to try to make up for that failure. – Charles Duffy Nov 05 '21 at 20:10
  • @CharlesDuffy that's why I asked wich operative system, that he could try it on Windows. Suhas found the answer using curly braces for the special characters, so he can update their question and add the answer. – Rafa Moyano Nov 05 '21 at 20:49
  • @RafaMoyano, the question is tagged bash. bash-on-Windows follows these rules just as much as bash-on-Linux does; if it's different on Windows, that'll be for non-bash shells (powershell, cmd.exe, etc), but those aren't within the scope of a bash-tagged question. – Charles Duffy Nov 05 '21 at 20:53
  • (also, answers should never be edited into questions; see [How best to deal with self-answers edited into questions?](https://meta.stackoverflow.com/questions/402010/how-best-to-deal-with-self-answers-edited-into-questions) on [meta]) – Charles Duffy Nov 05 '21 at 20:53
  • @CharlesDuffy thanks for the explanation. – Rafa Moyano Nov 05 '21 at 20:59