0

I have a bash script that will temporarily store a string and uses it later within a command. The issue is that a lot of times the string will have ! or other non-alphanumerical characters and bash will then take them literally and does what bash would usually do with these characters.

So for the example of !, it will try to run a command by doing History Substitution. I know that for that specific case I can just start the script with #!/bin/bash +H, but I can't really account for all the possible characters, so how can I make bash use the values within a variable literally?

For example:

var=u134y123!3q54wtgf..314/sa|asd
/path/to/command -option $var

In this above case, how would I make it so bash doesn't think I'm doing History Substitution or I/O Redirection (due to the |)?

AnthonyBB
  • 351

2 Answers2

3

It's all about quoting. Single-quote ! and | to store them literally. Double-quote $var.

var='u134y123!3q54wtgf..314/sa|asd'
/path/to/command -option "$var"
0

Not sure if I do understand you correctly. To me it seems

var="u134y123ls -laq54wtgf..314/sa|asd"
echo "$var"

does the trick. Output:

u134y123ls -laq54wtgf..314/sa|asd
Durtal
  • 101