I have a command I need to run in a bash script that takes the output of a previous command and inserts it into a new command.
So for example:
VARIABLE=$(cat "file.txt" | grep "text")
Then use that variable in another command:
mycommand -p "$VARIABLE"
The catch is the $VARIABLE will always contain special characters, namely $ and / which I need so I need to single quote that so the special characters are taken literal.
I've tried \""$VARIABLE"\" which didn't work.
What I'm trying to accomplish is I need to grab a line out of a text file that includes my search term, which is unique and only one line will have it.
I then need to input that line (well, half of it, I'm also cutting the line and using the second half) into another command. I am able to successfully grab the text I need which I verified by echoing the variable afterwards. The problem is the variable contains $ and \ that are being interpreted as special characters.
For example:
my command -p $345$randomtext\.moretext
Without single quoting the variable it is interpreted which throws errors.
printf "%s\n" my command -p "$VARIABLE"show that the special characters are being interpreted?VARIABLEis set to$345$randomtext\.moretext, thenmy command -p "$VARIABLE"is equivalent tomy command -p '$345$randomtext\.moretext'(as desired), not tomy command -p $345$randomtext\.moretext. So the problem must be somewhere inside the definition ofmy command.echocan be very misleading, since it prints its arguments after the shell has interpreted and removed quotes, escapes, etc. Tryecho my command -p '$345$randomtext\.moretext'-- you'll see that it doesn't include the quotes in the output, exactly the same asmy command -p "$VARIABLE". You can useset -xto get the shell to print the simplified equivalent of each command as it executes it to get a better idea what's really going on.