0

Why does the following here document throw error instead of creating ~/Desktop/test.sh?

sed: /first_names.txt: No such file or directory

cat << EOF > ~/Desktop/test.sh
#! /bin/bash

set -e

first_name=`sed "$(jot -r 1 1 2048)q;d" $dirname/first_names.txt | sed -e 's/[^a-zA-Z]//g'`

echo #first_name
EOF
sunknudsen
  • 6,356
  • 3
  • 39
  • 76
  • 1
    the contents of heredoc are expanded before being written to `test.sh`. You need to quote the heredoc to avoid this expansion - `'EOF'` – Inian Mar 04 '20 at 15:44
  • `$dirname` is empty and so `$dirname/first_names` is reduced to `/first_names` while writing the script. I assume, that the file does not exists in the root directory. – Wiimm Mar 04 '20 at 15:46
  • @Inian Thanks! Didn’t know heredoc’s were expanded. Please submit an answer. – sunknudsen Mar 04 '20 at 15:47

0 Answers0