-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!

closed as unclear what you're asking by jasonwryan, Gilles, peterh, Stephen Rauch, αғsнιη Nov 4 '17 at 3:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

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.