Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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'm trying to automate creating vhosts on my computer. This is merely a learning experience for bash scripting. I'm currently novice. I'm trying to learn more about awk, sed.

No, this isn't homework. I work full-time.

Anyways, this is my conf file. What would be the most efficient way to find and replace from command line? I'll eventually replace some forms with tokens, like {DOMAIN} and {PATH}

NameVirtualHost commerce.l:*

<Directory "/home/chris/workspace/dev.commerce/html">
        Options Indexes Includes execCGI
        AllowOverride All
        Order Allow,Deny
        Allow From All

<VirtualHost commerce.l>
    DocumentRoot /home/chris/workspace/dev.commerce/html
    ServerName commerce.l
    ErrorLog logs/commerce.error
    <IfModule mod_rewrite.c>
        <Directory "/home/chris/workspace/dev.commerce/html">
            RewriteEngine on
            # needed by Drupal 7 for "clean URLs"
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_URI} !=/favicon.ico
            RewriteRule ^ index.php [L]

P.S. I'm on Ubuntu 11.04 natty

share|improve this question
up vote 1 down vote accepted

This looks like a job for a here-document: include the template in your script, and use $variable_name when you want to substitute variables, or $(shell-command) to substitute the output of any shell command.

The here-document begins on the line after the marker <<EOF (you can replace EOF by any word) and ends on a line containing exactly EOF (no indentation allowed). Inside the template, the same characters are special as inside double quotes: "$`\ (note the backquote, which needs to be protected \`).

cat >>/etc/apache/sites-available/$DOMAIN
NameVirtualHost $DOMAIN:*

<Directory "$PATH">
share|improve this answer
Do you know of any other web resources relating to this? I'm trying to find some good examples. – chrisjlee Jun 30 '11 at 14:26
@Chris Read the Wikipedia article and your shell's manual and your favorite shell book. Google for “here document” or “heredoc”. – Gilles Jun 30 '11 at 15:58

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.