Bash Script Init Style Status Message

Have you ever wondered how the OK and/or FAILED messages are printed out in the initialization of services on a Linux system during bootup? Well… I have. Anyway, I’ve always thought that it was really cool how the OK and FAILED messages printed out at the same column on the screen and in different colors. So of course I wanted to know how to do that so I could use it in my own scripts.

The Source

If you look inside some of your systm init scripts you’ll notice that many of them source the /etc/rc.d/init.d/functions script. This is actually where I found the code to produce neato status messages.

The Code

I could have just sourced that file (included) in my scripts and been fine, but I wanted my scripts to be self-contained so I took the liberty of pulling out the applicable code. Here it is below with comments:


So you can just drop the above at the top of your script and then call the appropriate functions where needed. For example you could do something like this:

Pretty simple, eh? Now what type of script might you need something like this in? Check out my previous article How To Create A Bash Install Script, which is a perfect example of the type of script in which you may want these status type messages.

Thanks for stopping by! I hope that you’ll leave any comments that you have below.