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 ?

marked as duplicate by user88036, roaima, Isaac, Kiwy, Anthony Geoghegan Oct 2 '18 at 10:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @don_crissti that is not duplicate, in many ways it is the opposite. – ctrl-alt-delor Oct 1 '18 at 18:01
  • so this is not a duplicate question ? – Lukas Oct 1 '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?. Note that only one answer there is correct. 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 1 '18 at 19:26
  • @don_crissti ^^ there you are – roaima Oct 1 '18 at 20:28
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.

  • 1
    @Kusalananda my bad, I was not thinking straight. I have now fixed my answer. – ctrl-alt-delor Oct 1 '18 at 18:10
  • Not any quotes....straight quotes. – Rui F Ribeiro Oct 1 '18 at 18:42
  • 1
    @RuiFRibeiro single (') of double ("), back quotes are deprecated, in favour of $() – ctrl-alt-delor Oct 1 '18 at 18:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.