I need to output some text as bash script, but in a script. I use cat for this, but it has one drawback. It interprets variables and stuff during it is being written. I do want to prevent this.
How to do that without quoting all varibles (my script is failrly long)? Example
cat >/tmp/script << EOF $HOSTNAME # lots of other stuff I do NOT want to escape like \$VARIABLE # ... EOF cat /tmp/script myhostname.mylan
I want:
cat /tmp/script $HOSTNAME
Edit: Please note my script (here only $HOSTNAME) is very long, I dont want to change it all. Also single quoting does not work with <<
cat >/tmp/script '<< EOF $HOSTNAME EOF' File not found: EOF'
What's the trick? Thanks.
Normally, $ symbol is used in bash to represent any defined variable. But if you use escape in front of $ symbol then the meaning of $ will be ignored and it will print the variable name instead of the value. Run the following commands to show the effects of escape character (\).
Bash uses whitespace to determine where words begin and end. The first word is the command name and additional words become arguments to that command.
Use the break statement to exit a while loop when a particular condition realizes. The following script uses a break inside a while loop: #!/bin/bash i=0 while [[ $i -lt 11 ]] do if [[ "$i" == '2' ]] then echo "Number $i!" break fi echo $i ((i++)) done echo "Done!"
If you want everything quoted:
cat << 'EOF' stuff here with $signs is OK as are `backquotes` EOF
See the section on "here documents" in the manual.
Escape the $:
cat >/tmp/script << EOF \$HOSTNAME EOF
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With