BASH Programming

Learn Bash Programming

Hi there. Are you new to the world of linux and trying to get around the shell? You want to become more effective hacking around in the terminal? You want to start scripting and automating repeated jobs. You want to learn bash programming and become a GURU?

Ok Great! Let’s do it.

Start with learning some of the basic commands that you can run from the shell. Some commands you will want to learn include: date, ls in order to look at files and directories, rm to remove files, mkdir to create a new directory, whereis to find a program or utility’s path that you are looking for, chmod to set permissions, chown to set ownership, perform commands on multiple targets with wildcards, and then find files you are looking for with find command.

Manipulating strings and numbers will be a common task. Compare strings to each other, force strings to lower case and uppercase, learn proper escaping of strings, string encoding when needed, Convert hexadecimal to decimal format, Globbing strings to find expected patterns, Arithmetic operations, loop through a list of strings in a script, and return a string as the result of a function.

Now if you are ready to get dirty with data, lets learn some more advanced commands such as cut, grep, awk, uniq, and tr to manipulate streams of data. These articles show multiple examples of how these commands and shift and sift through what you are looking for in files.

You are going to want to make your scripts professional and fancy you will need to parse the command line arguments of the script. Getopts can help with parsing, you can create professional menu options with the select command and you can wait for user input, and the read it from the user with the read command. You can also make it more professional looking playing with colors in the terminal.

Now in your scripts you want to do some actual coding and programming to make complex logic in the script so you will need some programming concepts such as storing command results in variables, conditional statements like if and else and the case command. Loops in bash allow you to iterate through large jobs of actions. You can do for loops and while loops in bash. Arrays are commonly used in programming languages to store sequences of element data. Arrays can also be used in bash. You can even make bash into a more strongly typed programming environment using the declare command.

Any professional bash person should also be familiar with environment variables, bash history and bash alias to setup and use the shell more effectively. Be sure to have that knowledge.

Reading from files is critical in bash jobs. Some of the tasks you might want to learn include reading a file line by line or using the head and tail commands to read just the beginning or end of a file. If you have JSON data in a file and want to parse it you can use the jq command for that.

Interacting with websites and web resources you can use the curl command, or the mail command to send an email from the shell.

Timing and dynamic interaction of scripts with real world events can be tricky. There are numerous tricks, techniques and commands in bash to help you automate event handling. For example the yes command, so you don’t have to type ‘yes’ but have it programmatically respond to commands that ask for confirmation. To pause or sleep in a script master the sleep command, or subtlety different the wait command, Run multiple commands in one bash line using pipes, AND, and OR operators to sequence and combine tasks. Keep shell sessions open even if you close the window with the Screen command or the nohup command.

You want to see a variety of typical scripts in action check out 30 examples of bash scripts.

Or if you want to get fancy look at these fancy techniques to impress your boss or colleagues: iterate over sequences generated on the shell, learn about HEREDOC, tput, printf and shell expansions how to create awesome outputs with bash scripts, or use inotify and rsync to create a live backup system using only a while loop in a bash script.

Finally don’t forget to add comments to your bash scripts! It helps others to read your scripts and it helps you when you come back to them after some time for sure!

About the author

Linux Wolfman

Linux Wolfman is interested in Operating Systems, File Systems, Databases and Analytics and always watching for new technologies and trends. Reach me by tweeting to @linuxhint and ask for the Wolfman.