4

I have one variable, which is coming from some where like:

VAR1='hhgfhfghhgf"";2Ddgfsaj!!!$#^$\'&%*%~*)_)(_{}||\\/'

Now i have command like this

./myscript.sh '$VAR1'

I am getting that $VAR1 from some diff process and when I display it look exactly as its above.

Now that command is failing as there is already single quote inside variable. In the process where I use it it is expanded at that point, which causes that error.

I have control over myscript.sh but not above command.

Is there any way I can get variable inside my script?

7
  • Variables in bash don't take a space on either side of =. It shouldn't be var = 1 but var=1 Commented Sep 6, 2018 at 6:19
  • @Inian Plus the variable on the left side is written without the dollar sign. That said, I 'm curious about answers. Commented Sep 6, 2018 at 6:26
  • ./myscript.sh '$VAR1' can't fail by itself because the variable is not being expanded. Commented Sep 6, 2018 at 6:36
  • 1
    Did someone mess up an eval statement? Please include a MCVE showing the error Commented Sep 6, 2018 at 6:43
  • 1
    @Master : Please be more precise in your question. There is nothing syntactically incorrect in the two statements you presented here, so if anything is possibly failing, it must be during the evaluation of myscript.sh, but since we don't know what is inside this script, we can not tell what's going wrong. Asider from this, the whole concept of passing the string $VAR to the script (instead of the content of the variable VAR looks odd to me, but you will probably know what you are doing here.... Commented Sep 6, 2018 at 10:12

1 Answer 1

5

What you are saying is not possible to failing when passing to your script. Might your script has processing issue (or a command where this argument will passing into it) which cannot expand the variable correctly. You can either use printf with %q modifier to escape all special characters then pass it to your script:

./myscript.sh "$(printf '%q\n' "$VAR1")"

... or do the same within your script before you wanted to pass to some other commands:

VAR2="$(printf '%q\n' "$VAR1")"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.