0

I'm trying to create a script that creates an other script that uses $1 and $#, the problem is that those variables are being interpreted by the first script, so they are empty. Here's my problem, the first script creates the script /tmp/test.sh

#!/bin/bash

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

echo $1
echo $#
EOF

The result in /tmp/test.sh:

#!/bin/bash

echo 
echo 0

Does anyone know how to avoid this and get in /tmp/test.sh $1 and $#?

I would like to have in /tmp/test.sh:

#!/bin/bash

echo $1
echo $#

Thanks in advance.

1 Answer 1

1

Quote the here-document delimiter so that the contents of the here document are treated as literal text (i.e., as if occurring in a single-quoted string).

cat << 'EOF' > /tmp/test.sh 
#!/bin/bash

echo $1
echo $#
EOF

Any quoting will work, not just single quotes. The only important thing is that at least one character be escaped.

  • cat << \EOF
  • cat << "EOF"
  • cat << E"O"F
  • etc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.