0

I have variables $a, $b, ... that I am trying to pass into a command in a script:

    helm upgrade -f ./values.yaml / 
    --set db.auth_key=$a /
    --set ...=$b /
    ... etc

If I echo $a I get the string (a salt) *|T}pE%j?ZHl|4#JgyXoW^y fzmadUMZ=H;|2FU2*!o_{6JH^}zIDj^:=:x.VjA-. The problem here is that I would need to escape a few characters, namely '|', '*', and '#' among others as it ruins my script if I don't. But as these salts are generated on the fly, I wouldn't know which characters to escape. How can I make bash render these as raw strings, "pre escaped" if you will?

Momo
  • 318
  • 2
  • 11
  • 1
    You have to quote: `"$a"`, `"$b"`. – Benjamin W. Dec 15 '20 at 00:46
  • 1
    Always run your scripts through https://shellcheck.net to find basic issues like that. – Shawn Dec 15 '20 at 00:52
  • Does ["When to wrap quotes around a shell variable?"](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) answer your question? – Gordon Davisson Dec 15 '20 at 00:54
  • There's a difference, in shell, between quotes that are *syntax* and quotes that are *data*. Quotes that are data cannot substitute for missing quotes that are syntax -- so if the reason you're trying to add quotes to your data is to cause _the shell itself_ to parse that data correctly, it's both unnecessary and doomed to fail: If you add _syntactical_ quotes to the script itself they'll do the right thing; whereas adding quotes to the data will _never_ work. See [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) for discussion of some of the effects of this distinction. – Charles Duffy Dec 15 '20 at 01:12
  • @GordonDavisson No, I did try all of these different combinations with the quotes and whatnot. It appears my problem is something more nuanced, and I have to figure it out before I can ask the right question to get the right answer. Perhaps my issue is a helm one https://github.com/helm/helm/issues/4030. – Momo Dec 15 '20 at 02:46
  • 1
    @Momo There are at least three places where special characters might be causing trouble: #1- where you set the variables, #2- where you use the variables as part of the `helm` command, and #3- inside `helm` itself. Double-quoting the variable references (`--set db.auth_key="$a"` etc) prevents trouble at step #2. To see if there's a problem at #1, print the variables' values with e.g. `printf "%s" "$a" | xxd` and see if it shows what it should (note: `echo $a` can be misleading for a number of reasons; don't use it). If that's right, there's something going on in `helm`. – Gordon Davisson Dec 15 '20 at 03:49

0 Answers0