Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if there is a better way to create a file other than appending each line to the file. I did it this way so I can preserve readability, however, there is no indentation. Is there a way to create a file and input multiple lines at once?

if [ -d "/srv/www/$1" ]; then
 echo "Domain name already exists!"
 mkdir -p /srv/www/$1/public_html; 
 mkdir -p /srv/www/$1/logs; 
 echo "<VirtualHost>" > /etc/apache2/sites-available/$1
    echo "ServerAdmin support@$1" >> /etc/apache2/sites-available/$1
    echo "ServerName $1" >> /etc/apache2/sites-available/$1
    echo "ServerAlias www.$1" >> /etc/apache2/sites-available/$1
    echo "DocumentRoot /srv/www/$1/public_html/" >> /etc/apache2/sites-available/$1
    echo "ErrorLog /srv/www/$1/logs/error.log" >> /etc/apache2/sites-available/$1
    echo "CustomLog /srv/www/$1/logs/access.log combined" >> /etc/apache2/sites-available/$1
 echo "</VirtualHost>" >> /etc/apache2/sites-available/$1
 a2ensite $1
share|improve this question
up vote 7 down vote accepted

Use a heredoc.

cat > /etc/apache2/sites-available/"$1" << EOF
ServerAdmin support@$1
share|improve this answer
8 minutes until you get best answer – Doug Oct 31 '10 at 4:21

Heredoc as stated in answer #1 or just have your echo go across multiple lines

echo "line 1
line3" > file
share|improve this answer

If it's readability you're after, have you considered splitting it out into multiple files? Have a "template" file that you can edit that is copied over with your shellscript.

## /path/to/vhtemplate
    ServerAdmin support@#1
    ServerName #1
    ServerAlias www.#1
    DocumentRoot /srv/www/#1/public_html/
    ErrorLog /srv/www/#1/logs/error.log
    CustomLog /srv/www/#1/logs/access.log combined

Example script:

if [ -d "/srv/www/$1" ]; then
 echo "Domain name already exists!"
 mkdir -p /srv/www/$1/public_html; 
 mkdir -p /srv/www/$1/logs; 
 cp /path/to/vhtemplate /etc/apache2/sites-available/$1
 sed -i -e 's/#1/$1/' /etc/apache2/sites-available/$1
 a2ensite $1
share|improve this answer
I like this a lot, and I upvoted your answer. Using templates is a great idea too. – Doug Nov 1 '10 at 7:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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