Search code examples
shelleofcatdollar-sign

Shell use variable and escape dollar character at the same time


How can i use a variable and escape a dollar character at the same time in a shell script using cat << _EOF_?

The important thing is there is no " or ' character i'm using.

Here is my text to write:

cat <<- _EOF_ >> "$serverconfigdir/$siteaddress"
    This is a sample text! Here is my variable below!
    $usethisvariable

    This is another sample text! $escapethisstring show it how it looks bla bla.
_EOF_

Solution

  • You need to escape the $ in 2nd occurrence:

    cat <<- _EOF_ >> "$serverconfigdir/$siteaddress"
        This is a sample text! Here is my variable below!
        $usethisvariable
    
        This is another sample text! \$escapethisstring show it how it looks bla bla.
    _EOF_