2

I write bash cat << 'EOF' > script to create files.script as below:

#!/bin/bash for sitename in (site1,site2,site3) do cat << 'EOF' >/home/$sitename/conf DEPLOY_DIR="/var/www/$sitename" git --work-tree="DEPLOY_DIR" EOF done 

The result after run this script should like:

[root@localhost]cat home/site1/conf DEPLOY_DIR="/var/www/site1" git --work-tree="$DEPLOY_DIR" 

The key is I need to substitute $sitename in DEPLOY_DIR="/var/www/$sitename" and keep git --work-tree="DEPLOY_DIR" as same.
How to do it?

2
  • @Inian,if I use << EOF,"$DEPLOY_DIR" will become "" Commented Jan 30, 2019 at 7:03
  • I added a new answer to the duplicate explaining how to avoid expansion of some strings. Also the for loop looks wrong here -- you want for sitename in site1 site2 site3; do Commented Jan 30, 2019 at 7:07

1 Answer 1

5
#!/bin/bash for sitename in site1 site2 site3;do cat << EOF > /home/$sitename/conf DEPLOY_DIR="/var/www/$sitename" git --work-tree="\$DEPLOY_DIR" EOF done 
Sign up to request clarification or add additional context in comments.

2 Comments

That's workable, but if you have lots of $variables , hard work.
This is the only way you will be able to extrapolate some variables while not extrapolating others. The other way would require more typing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.