Recently I tried passing a bash variable to perl command in bash script, it didn’t end well.

Troy Engel from was nice enough to point out the issue:

use sed instead of perl for what you need; it’s simpler, faster and uses the bash variables easily.

I set up a test script /home/someuser/ to show:

# cat test.conf
# cat

sed -i "s|domain|${domain}|g" "${PATH_DEST_CONFIG}"
# sh -x ./
+ domain=test
+ PATH_DEST_CONFIG=/home/someuser/test.conf
+ sed -i 's|domain|test|g' /home/someuser/test.conf
+ sed -i 's|PATH_DEST|/test|g' /home/someuser/test.conf
# cat test.conf

What’s important to note is that A, you *must* use double-quotes in the sed expression so that variable expansion works; single-quotes inhibits expansion. B, as you found out, you need to use something other than “/” as your expression separator since you have a replacement variable with that character in it. I used pipes in sed, but you could use almost anything (“#”, etc.) for the same effect.

Last modified: January 18, 2013



Write a Reply or Comment

Your email address will not be published.

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

This site uses Akismet to reduce spam. Learn how your comment data is processed.