Following up on your previous question, it sounds like you want both some uninterpreted text and some interpreted text going into a file. In that case, use two different cat
s (or echo
s):
cat > /test <<'EOF'
some uninterpreted text $(pwd)
not substituted: $1
EOF
cat >> /test <<EOF
but this will substitute: $1
EOF
There's a couple of things going on here: firstly, the heredoc syntax with <<
. If you include quotes in that terminator string, as in the first one above, the entire heredoc is uninterpreted — no parameters and no substitutions. If you don't use any quotes, like in the second cat
above, variables like $1
will be replaced by their values and command substitutions will be included in the text. You choose between whether to quote the "EOF" string or not based on whether you want substitutions or not.
To put both cat
s into the same file we're using >>
redirection for the second (and any later) redirections: that means to append to the file. For the first one, we use a single >
to clear the file out and start fresh.
Note that, even when variables are substituted, any additional dollar signs in that variable's value aren't re-substituted themselves:
foo='$bar'
bar=hello
cat <<EOF
$foo
EOF
will output:
$bar
without substituting in $bar
's value.
However, if you're providing a "$" in the argument to this whole script, you need to escape it on the command line or enclose the whole thing in single quotes. Alternatively, command substitution as in your other question lets you put the contents of a whole file in directly, including any dollar signs in the file contents. Make sure you quote the substitution string there:
oo.sh "$(cat myfile)"
will get the body of myfile
as $1
and can then cat
or echo
it as required. The same limitations as in my answer there apply: there's a limit for how long the command-line arguments can be, and if your file might get longer than that you should find another approach. You can find out what the limit is on your system with getconf ARG_MAX
$1
) contains dollar signs.' Not if it refers to the first command line parameter. Substitution was performed on that before your script executed. You have to escape it on the command line. – goldilocks Jun 21 '14 at 09:27cat
and keep dollar signs”, but “I want to modify thehttpd.conf
to change"$foo"
and not$foo
. – Gilles 'SO- stop being evil' Jun 21 '14 at 12:29$1
to a file like this:echo "$1" > file
. That works. Your problem, as Gilles has been trying to tell you, is elsewhere. – derobert Jun 21 '14 at 17:40