2

This is my script:

var="lalallalal"
tee file.tex <<'EOF'
text \\ text \\
$var
EOF

Need to use 'EOF' (with quotes) because I can not use double slash (//) otherwise.

However, if I use the quotes, the $var variable is not expanded.

2

If you quote EOF, variables won't be expanded, as you found out. This is documented in the Here Documents section of man bash:

   No parameter and variable expansion, command  substitution,  arithmetic
   expansion,  or pathname expansion is performed on word.  If any part of
   word is quoted, the delimiter is the result of quote removal  on  word,
   and  the  lines  in  the  here-document  are  not expanded.  If word is
   unquoted, all lines of the here-document  are  subjected  to  parameter
   expansion,  command substitution, and arithmetic expansion, the charac‐
   ter sequence \<newline> is ignored, and \ must be  used  to  quote  the
   characters \, $, and `.

The other thing explained there is that you can still use\, you just need to escape it: \\. So, since you want two \, you need to escape each of them: \\\\. Putting all this together:

#!/bin/bash
var="lalallalal"
tee file.tex <<EOF
text \\\\ text \\\\
$var
EOF
0

It seems you want to have expansion enable only for part of document, or only on some of special characters ($ yes, \ no). In the first case you may want to divide here-document into two, and in the second case let me propose non-standard approach: treat \ as a variable:

var="lalallalal"
bs='\';                       #backslash
tee file.tex <<EOF
text $bs$bs text $bs$bs
$var
EOF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.