cat >run_pos2bed3.sh <<EOF ls 2*/peaks.txt | while read id; do echo $id done; EOF
after input it
nano only show
ls 2*/peaks.txt | while read id; do echo done; EOF
How can I add
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
$iddisappear after the
Because, when you say
<< and then
$, you get the value that the variable already has.
If you do
id=foo cat >run_pos2bed3.sh <<EOF ls 2*/peaks.txt | while read id; do echo $id done; EOF
you'll see that you get
do echo foo done.
How can I add
cat >run_pos2bed3.sh << \EOF ls 2*/peaks.txt | while read id; do echo $id done; EOF
The difference is the
\ before the
EOF on the
The contents of a here-document will undergo expansion (of variables and command substitutions) unless the document is quoted. In your case, the here-document is unquoted, so
$id is expanded. It is likely that the variable was unset when the document was redirected, so its value is expanded to an empty string. This makes it appear as if the variable "disappeared".
cat >run_pos2bed3.sh <<'END_SCRIPT' ls 2*/peaks.txt | while read id; do echo "$id" done END_SCRIPT
or, for a safer script (the
id variable is actually not needed),
cat >run_pos2bed3.sh <<'END_SCRIPT' printf '%s\n' 2*/peaks.txt END_SCRIPT
It's the single quotes in
'END_SCRIPT' that makes the here-document quoted (this could also be written as either
I've also quoted the expansion of
$id within the document itself, as is required by good shell scripting practices (see e.g. When is double-quoting necessary?).