1

Problem:

I have an escaped string saved within a variable:

escapedFileName='/dbDumps/Mon\ Oct\ \ 1\ 15\:22\:50\ UTC\ 2018.sql'

but whenever I try to use this file name within the following command, I get an error message saying that this path does not exist (even though it does).

/usr/bin/mysql -u root -pmypassword system < "$escapedFileName";

When i use the path and not the string it works :

/usr/bin/mysql -u root -pmypassword system < /dbDumps/Mon\ Oct\ \ 1\ 15\:22\:50\ UTC\ 2018.sql

What am I doing wrong ?

roaima
  • 107,089
  • 14
  • 139
  • 261
Lukas
  • 13
  • 2
  • @don_crissti that is not duplicate, in many ways it is the opposite. – ctrl-alt-delor Oct 01 '18 at 18:01
  • so this is not a duplicate question ? – Lukas Oct 01 '18 at 19:02
  • 1
    There's always a duplicate out there but someone has to find it... Here's a better candidate (I can no longer vote to close this one): [Why I can't escape spaces on a bash script?](https://unix.stackexchange.com/q/108635/22142). Note that [only one answer there is correct](https://unix.stackexchange.com/a/108663/22142). Both _braiam_'s and _daisy_'s answers are _wrong_ (as you already know now) - double quoting the variable doesn't solve this problem. Not surprisingly, one of those answers is accepted and has an insane number of upvotes... – don_crissti Oct 01 '18 at 19:26
  • @don_crissti ^^ there you are – roaima Oct 01 '18 at 20:28

1 Answers1

1

You don't need to escape when you put the text between quotes.

Therefore remove the \s

escapedFileName='/dbDumps/Mon Oct  1 15:22:50 UTC 2018.sql'

And you should not escape the :, they don't need it, and it will break it.

ctrl-alt-delor
  • 27,473
  • 9
  • 58
  • 102