Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent bash from interpreting without quoting everything

Tags:

bash

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.

like image 609
lzap Avatar asked Mar 26 '12 09:03

lzap


People also ask

How do you pass variables in single quotes in bash?

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 (\).

Does bash ignore whitespace?

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.

How do I stop a shell script from looping?

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


2 Answers

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.

like image 64
torek Avatar answered Sep 23 '22 04:09

torek


Escape the $:

cat >/tmp/script << EOF   \$HOSTNAME EOF 
like image 45
Eran Ben-Natan Avatar answered Sep 22 '22 04:09

Eran Ben-Natan