-2

While running the command through script, Bash is expanding TLS_CERT variable with extra single quotes, which is making my command to fail.

I have put simplified code here for readable format

Can anybody please suggest how to avoid this.

Code:

TLS_CERT="--set-file \"global.vertica.cert.vertica-ca\.crt\"=./vertica-1030.crt"

helm install opsb -n tns opsbridge-suite-2020.08.0-1-SNAPSHOT.tgz -f vert-opsb-values.yaml $TLS_CERT

Expected output :

helm install opsb -n tns opsbridge-suite-2020.08.0-1-SNAPSHOT.tgz -f vert-opsb-values.yaml  --set-file "global.vertica.cert.vertica-ca\.crt"=./vertica-1030.crt

Actual Output:

helm install opsb -n tns opsbridge-suite-2020.08.0-1-SNAPSHOT.tgz -f vert-opsb-values.yaml  --set-file '"global.vertica.cert.vertica-ca\.crt"=./vertica-1030.crt' 
Digvijay S
  • 2,665
  • 1
  • 9
  • 21
Krohit
  • 1
  • Welcome to SO. Please use "`" to format code – Digvijay S Jun 12 '20 at 09:57
  • 2
    how are you getting actual output ? `'"global.vertica.cert.vertica-ca\.crt"=./vertica-1030.crt' ` Also, your variable is getting enclosed in `'`. No extra `'` at the end – Digvijay S Jun 12 '20 at 10:01
  • this works: `TLS_CERT='--set-file "global.vertica.cert.vertica-ca.crt"=./vertica-1030.crt'` – Ron Jun 12 '20 at 10:03
  • Correct. That's extra single quote is getting added by the bash – Krohit Jun 12 '20 at 10:03
  • @Ron I checked it, it doesn't work – Krohit Jun 12 '20 at 10:09
  • I think presence of double quote and \ in variable TLS_CERT is causing the problem. But that are required for my command to execute correctly. – Krohit Jun 12 '20 at 10:11
  • are you using `#!/bin/bash` or something else? And which version of bash? – Ron Jun 12 '20 at 10:19
  • What do you want to run? I doubt you want to add doubleqoutes there, why are you adding them? No, qouting is interpreted once, once qoutes are removed, the result is not re-evaulated again - after that the content is split on whitespaces and taken literally. So if the veriable contains qoutes literally, there will be taken literally. Do you want to execute `program "\"global...\""` or `program global...`? – KamilCuk Jun 12 '20 at 10:21
  • @Ron bash --version GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later – Krohit Jun 12 '20 at 11:40
  • @KamilCuk I need to pass the certificates in helm in that global variable. I want to execute program --set-file "global.vertica.cert.vertica-ca\.crt"=./vertica-1030.crt – Krohit Jun 12 '20 at 11:42
  • 1
    I am on a box with the exact same bash version, and I am unable to replicate your error... the only time I see single quotes is when I use `set -x` in the script to see what is parsed, which is normal... but when I execute the script as you have posted in your Question, I do not see single quotes.. – Ron Jun 12 '20 at 12:33
  • How are you seeing actuual output? – stark Jun 12 '20 at 15:10
  • @Ron Yes you will not be able to see it by echo, but when you use set -x, it is clearly shown that it is adding extra single quote. earlier I thought that it is normal with -x option and it will not affect the actual command run, but actually it's affecting and the command is running with single quotes, which resulting into failure. – Krohit Jun 13 '20 at 05:50
  • @stark By using -x option. – Krohit Jun 13 '20 at 05:51
  • @Krohit `-x` is showing you parsing of a `variable that`... did you actually try to run your command, or you are looking at it only to `see` the `'` .. and if you did, HOW is it different from manually running the whole command? – Ron Jun 13 '20 at 12:17
  • @Ron, i did ran it manually without single quote and it's working fine but when running through script it's failing the same way as It fails when running it manually with single quotes. – Krohit Jun 13 '20 at 15:00
  • Well, you never shared what the actual error is that you are getting. "it's not working" is not helpful at all to troubleshoot. – Ron Jun 13 '20 at 23:20

1 Answers1

0

As usual with such problems, use bash arrays.

tls_cert=( --set-file global.vertica.cert.vertica-ca.crt=./vertica-1030.crt )
program ... "${tls_cert[@]}"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • it doesn't work, I need global.vertica.cert.vertica-ca.crt in double quote and the moment I add that it again causes the same- addition of single quote. This is what I got: --set-file '"global.vertica.cert.vertica-ca.crt"=./vertica-1030.crt' – Krohit Jun 12 '20 at 11:37