# How do you parse a variable in a bash script?

**URL:** <https://community.unix.com/t/how-do-you-parse-a-variable-in-a-bash-script/189486>\
**Category:** Shell Programming and Scripting\
**Created:** [March 18, 2008, 3:38pm UTC](https://community.unix.com/t/how-do-you-parse-a-variable-in-a-bash-script/189486 "2008-03-18T15:38:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vertical98](https://community.unix.com/letter_avatar/vertical98/32/6_22734026db12803ebb3e059622ac3534.png) [@vertical98](https://community.unix.com/u/vertical98)\
**Post date:** [March 18, 2008, 3:38pm UTC](https://community.unix.com/t/how-do-you-parse-a-variable-in-a-bash-script/189486/1 "2008-03-18T15:38:30Z")

</div>

I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes.

This is what I use on my apache server

#!/bin/bash  
# accepts 3 parameters: \<domain name\> \<user name\> \<XXXXXXXX\>  
# domain name is without www (just [domain.com](http://domain.com))  
# username would be best at 6 - 10 chars long  
# only checks if last is present and uses it for MySQL  
# password. If not present - does not create mysql account  
if ["$3" != ""]; then

--- snip ----

useradd $2 -m  
filename=/etc/apache2/sites-available/$2.www  
echo "\<VirtualHost 10.10.10.10\>" \> $filename  
echo "ServerAdmin webmaster@$1" \>\> $filename

--- snip some more -----

I now need to parse the first variable to add some characters:

So basically:

using [example.com](http://example.com)

#  
# [example.com](http://example.com)  
#  
$HTTP["host"] =~ "(^|\.)example\.com$" {  
server.document-root = "/home/example/public\_html"  
server.errorlog = "/var/log/lighttpd/example-error.log"  
accesslog.filename = "/var/log/lighttpd/example-access.log"  
server.error-handler-404 = "/e404-example.php"  
}

Needs to become:

echo "#" \>\> $filename  
echo "# %1" \>\> $filename  
echo "#" \>\> $filename  
echo "$HTTP[\"host\"] =~ \"(^|\.)\<domain\>\.\<tld\>$\" {" \>\> $filename  
--- etc ----

I think the best way would be to parse until I hit the period and use the first part as one variable and the last part as another. I hope this makes sense.  
Any assistance would be appreciated.

Alan

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [March 18, 2008, 3:49pm UTC](https://community.unix.com/t/how-do-you-parse-a-variable-in-a-bash-script/189486/2 "2008-03-18T15:49:12Z")

</div>

So something like this?

```plaintext
#!/bin/bash

domain=${1%.*}
tld=${1#*.}

cat <<HERE
#
# $domain.$tld
#
\$HTTP["host"] =~ "(^|\.)$domain\.$tld\$" {
server.document-root = "/home/$domain/public_html"
server.errorlog = "/var/log/lighttpd/${domain}-error.log"
accesslog.filename = "/var/log/lighttpd/${domain}-access.log"
server.error-handler-404 = "/e404-$domain.php"
}
HERE

```

Example run:

vnix$ /tmp/htt [example.com](http://example.com)  
#  
# [example.com](http://example.com)  
#  
$HTTP["host"] =~ "(^|\.)example\.com$" {  
server.document-root = "/home/example/public\_html"  
server.errorlog = "/var/log/lighttpd/exampleerror.log"  
accesslog.filename = "/var/log/lighttpd/example-access.log"  
server.error-handler-404 = "/e404-example.php"  
}

I hope I got all the details right.

You can't really "parse" the variables much, you can perform simple string substitutions like I have but basically it's just $1 $2 $3 and if you need anything fancier, use some external utility.

---

<div class="post-metadata">

**Author:** ![vertical98](https://community.unix.com/letter_avatar/vertical98/32/6_22734026db12803ebb3e059622ac3534.png) [@vertical98](https://community.unix.com/u/vertical98)\
**Post date:** [March 18, 2008, 4:21pm UTC](https://community.unix.com/t/how-do-you-parse-a-variable-in-a-bash-script/189486/3 "2008-03-18T16:21:04Z")

</div>

Thanks for the fast reply! The more I learn about BASH the more I realize I have to learn.

I'll try it out right now.

---

<div class="post-metadata">

**Author:** ![vertical98](https://community.unix.com/letter_avatar/vertical98/32/6_22734026db12803ebb3e059622ac3534.png) [@vertical98](https://community.unix.com/u/vertical98)\
**Post date:** [March 18, 2008, 4:30pm UTC](https://community.unix.com/t/how-do-you-parse-a-variable-in-a-bash-script/189486/4 "2008-03-18T16:30:37Z")

</div>

That does exactly what I needed. Again MANY thanks!
