0

I am trying to dynamically create and write to a file a bash script from from a current bash script.

echo "## Get the IP of the system that was assigned
#!/bin/bash 
ip=$( hostname -i | xargs )
echo $ip " > second_script.sh

rather echo out the lines it is executing them, so that I end up with expanded/evaluated variables in second_script.sh.

## Get the IP of the system that was assigned
#!/bin/bash
ip=10.0.2.15
...

How can I write out bash shell script commands and suppress evaluation, and be able to execute the script later on?

  • 1
    you may want to put the she-bang line as the first line, as well. – Jeff Schaller Apr 27 '16 at 19:20
1
echo $(hostname)

does not work, as you already realized, since the command is expanded. Use single quotes ' to prevent command expansion:

echo '$(hostname)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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