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

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, 2016 at 19:20

1 Answer 1

echo $(hostname)

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

echo '$(hostname)'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .