Thursday, August 1, 2013

bash - putting complicated strings/text into a variable

It is a usual practice to generate commands within a script and later execute them with eval. Or one simply want to put into a variable some weird string containing quotes and other special characters. To avoid having to quote or escape everything, just write what you want to see into the variable, here's a simple way to do it... looking at it seems so obvious but truth is I never thought about that approach before:
read -r myvar << "EOF"
STRING with quotes (') and other special characters like $ and ! and #
I came to it while trying to use sed to insert another sed command into a script. Here is how it ended up like:
read -r sed_cmd <<"EOL"
sed -i -e 's#<systemPropertyVariables#<systemPropertyVariables combine.children="append"#' testsuite/integration/*/pom.xml
sed -i -e "s%mvn%$sed_cmd\nmvn%"

No comments:

Post a Comment