0

I want to print a string wrapped in '{}'

VAR='something,somethingelse'
$BIN $OPTIONS --ignore-table={$VAR} $DB

Which produces:

+ /bin/mysqldump --all-databases '--ignore-table={something,somethingelse}' mydatabase 

Why is the --ignore-table parameter being quoted and how can I get it so mysqldump can handle it?

Thanks for the help!

D.Fitz
  • 493
  • 5
  • 16
  • 1
    The quotes are showing you how the shell parses it. They're not actually anything being literally added. – Charles Duffy Aug 29 '18 at 23:06
  • 1
    Brace expansions happens **before** parameter expansion, so curly braces are only honored prior to the point when variables have their values expanded. – Charles Duffy Aug 29 '18 at 23:07
  • Consider `ignore_options=( --ignore-table={something,somethingelse} )`, then `mysqldump "${ignore_options[@]}"` later. (BTW, using `BIN=mysqldump` is typically cargo-cult programming -- people following something they saw in a shell script from the 1970s without understanding why; modern shells cache PATH lookups, so there's no value whatsoever to putting full paths to your executables in variables -- just put the program's name directly in your script; if you wanted to override it, you could just do that with a shell variable). – Charles Duffy Aug 29 '18 at 23:10
  • ...similarly, your options should be an array, not a string: `options=( --whatever1 --whatever2="quotes work this way too" )`, expanded as `"${options[@]}"`; see http://mywiki.wooledge.org/BashFAQ/050 for discussion of some of the ways the other approach fails. – Charles Duffy Aug 29 '18 at 23:11
  • (to be clear, in the case of `--ignore-table={something,somethingelse}`, it *isn't* `mysqldump` that "handles it" -- the shell replaces it with `--ignore-table=something` `--ignore-table=somethingelse` *before* `mysqldump` is invoked; if you have an array, `ignored_tables=( something somethingelse )`, you could set `options=( ); for tbl in "${ignored_tables[@]}"; do options+=( --ignore-table="$tbl" ); done` and then expand `"${options[@]}"` into the mysqldump command line to have the desired effect). – Charles Duffy Aug 29 '18 at 23:27
  • This is great! I found using `eval` works as is, but I like your options and have used them instead. This is part of a backup script that makes several decisions before executing mysqldump. I shortened much of it to make the question as straight forward as possible. Thank you very much! Plus +1 – D.Fitz Aug 30 '18 at 14:47
  • *nod* -- `eval` parses data as code, so it introduces security risks -- as described in [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048) -- as well as correctness issues if not used carefully (compare `printf '%s\n' '*'` with `eval printf '%s\n' '*'`). – Charles Duffy Aug 30 '18 at 14:57
  • (oops, when I said "if you wanted to override that, you could just do that with a shell variable", I meant to say "you could just do that with a function" -- consider as an example `mysqldump() { command mysqldump.bin "$@"; }` to make any call to `mysqldump` really call `mysqldump.bin`; it's rare that that kind of thing is actually needed, but, well, that's part of why there isn't a good reason to use `MYSQLDUMP=mysqldump` or similar; substitutions *are* only rarely needed in practice). – Charles Duffy Aug 30 '18 at 14:59

0 Answers0