0

I've got a simple backup script that goes something like this...

OUTPUT_DATE=$(date +"%Y%m%d_%H%M%S")
LIVE_DATABASE=“MyDatabase”
FILE_DESTINATION=“~/apps/path/databases/dump_$OUTPUT_DATE.sql

mysqldump -u foobar -p123456 -h example.com -P 1234 $LIVE_DATABASE > $FILE_DESTINATION

But when executing the script, I get ~/apps/path/databases/dump_20230511_144827.sql does not exist - which of course it doesn't, I'm trying to generate the file.

However, if I don't use a variable, but the filename like this, it works:

mysqldump -u foobar -p123456 -h example.com -P 1234 $LIVE_DATABASE > ~/apps/path/databases/dump_$OUTPUT_DATE.sql

What am I missing?

dangvy
  • 316
  • 1
  • 11
  • 4
    Quotes prevent expansions. `~` is an expansion. You need `FILE_DESTINATION=~/apps/path/databases/dump_"$OUTPUT_DATE".sql` with the `~` outside the quotes. (On the right-hand side of an assignment, the double quotes are optional too). – Charles Duffy May 11 '23 at 15:17
  • 2
    BTW, don't use all-caps variable names for variables you define yourself; they're reserved for variables that reflect or modify system behavior. (For example, `PATH=something` will break your ability to run programs, but `path=something` is perfectly fine). – Charles Duffy May 11 '23 at 15:18
  • 2
    Also, use quotes on _every_ expansion. `"$LIVE_DATABASE"`, not unquoted `$LIVE_DATABASE`, f/e. http://shellcheck.net/ will catch many instances of this (though it needs to be reconfigured away from defaults a little to catch _all_ of them). – Charles Duffy May 11 '23 at 15:19
  • 3
    And your code had "smart quotes" inserted, maybe by some kind of tool trying to be "helpful". `“` is not a legal double quote character in shell, and neither is `”`; it needs to be `"` – Charles Duffy May 11 '23 at 15:19

0 Answers0