-6

Why does this Bash script not work, to print arguments from the command line?:

cat << EOF > test
echo "First arg: $1"
echo "Second arg: $2"
echo "List of all arg: $@"
EOF
bash test hello world!
1

Shell variable expansion .
Quote the EOF , so the cat program knows it is looking for a string .
Remove the ambiguity .

What you are using is called a 'here document' . heredoc .
A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.

Can turn off the shell expansion by quoting the EOF . 'EOF' .

Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/here-docs.html
Wikpedia :
https://en.wikipedia.org/wiki/Here_document

cat << 'EOF' > test
echo "First arg: $1"
echo "Second arg: $2"
echo "List of all arg: $@"
EOF
bash test hello world!

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