Linux

Why write bash scripts?

Bash programming is really two things combined. It’s bash and it’s programming. So we are going to talk about both the facets here. When it comes to Unix like operating system, if you are completely incompetent on the command line and you don’t know what command line tools to use to do stuff on Linux or Unix than your bash scripts aren’t going be as flexible as they really are. You will end up doing things the hard way and that’s gonna take very long time. It is important to have prior knowledge about some basic command line tools that are used in Unix like environment.

So what is bash scripting or programming?

Bash scripting or programming is taking that knowledge of how to work on a shell or how to do one or two things on a shell and scripting it in a single file or multiple files if needed. In bash scripting you don’t just write shell commands in script, you also make use of programming like ingredients such as if-else statements to control the flow of execution.

We are not going to discuss about the specific command line tools that you will use in your bash scripts which are just collection of statements that you could do on the shell. This is just an overview about what happens when you take those command line statements and put them together with some programming principles and try to write useful reusable scripts that saves you time and do things intelligently.

What’s bash good for?

Bash and shell scripting is really for light task automation. You are not gonna right your weather modeling software in bash. Although some people may argue and they use bash scripting as any other programming language. Of course a very talented developer can take something like bash and use it for something that it is not very good at and still make it work. We are not debating that. However the place where bash shines is not writing complex software. The place where bash shines is light task automation like doing your backups.

It’s for small repetitive jobs that you want to script and you want them to be reasonably portable so that you could use them again on any other Linux or Unix machine. So use bash scripting where it is appropriate and use other tools where they are appropriate. Thumb rule is that if you are gonna use more then two hundred lines of code in your scripts you might wanna consider some other tools like python, perl or ruby.

Prerequisites for bash

You need to know some shell basics. You should be aware about standard errors, standard in, standard out and file descriptors. For instance you should be comfortable with something like writing some text on shell and then redirecting it to the file or feeding output of one program to another. It’s good if you know some programming basics but it’s not essential.

In bash you can write code that is less optimized and more lines of codes which is more readable and easy to understand because the kind of tasks that you are gonna use bash for are already low level. So performance is going to be your last concern. You are going to be using C, you are going to be using something that is much lower level that lets you control the way memory is used manually. Something that is manually typed and that’s what you write your weather simulation models with.

Below are some directives given that you need to learn for bash scripting.

  • Basic file and directory operations
  • Working with files
  • Working With Processes
  • File Name expansions
  • Output Redirects
  • Controlling Execution

You can find these resources to learn at link given below.

Linux Commandline

About the author

Ajay Verma

A Computer Science Graduate, who works extensively on open source projects. His Areas Of interest are: Network Security, Linux Administration, FOSS, Python, and C programming.

Add Comment

Click here to post a comment