Over a million developers have joined DZone.

Fork Less in Bash and See Performance Wins

DZone's Guide to

Fork Less in Bash and See Performance Wins

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

So, if you haven’t seen this page you should take a look. It has a whole bunch of interesting techniques you can use to manipulate strings in bash. If you end up working with bash a lot you might find yourself doing this quite a bit, since it can save a lot of time.

Let's take a pretty typical task, stripping the domain off of an email address.

So this poorly written program will split an email address at the first @ and print the first portion:

while test $i -lt 1000; do
	echo ${STRING} | awk -F@ '{print $1}'

Its counterpart, which does not fork, uses a bash built-in to remove everything after the @:

while test $i -lt 1000; do
	echo ${STRING%%@*}

So, what's the execution difference?

$ time bash with_awk_split.sh > /dev/null
real	0m3.737s
user	0m0.196s
sys	0m0.556s
$ time bash with_bash_split.sh > /dev/null
real	0m0.034s
user	0m0.020s
sys	0m0.012s

It's 100x faster to skip the fork.

Now, granted this is a pretty dumb example, and it's easy to rewrite this to perform better than the bash example (i.e. don’t use a loop and just use awk which is 3x faster than the pure bash solution). So, think about what your doing, use a pipe over a loop, and if you can’t do that, try to find a built-in that can take care of your needs.

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs


Published at DZone with permission of Geoffrey Papilion, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}