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!

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

bash date examples

Bash uses `date` command to display or change the current date and time value of the system. Date and time value can be...

Shell scripts – What can you change

In most, if not all, shells, you have a script that starts your shell. Learn how to change it and you can have your own...

Bash Range: How to iterate over sequences generated on the shell

You can iterate the sequence of numbers in bash by two ways. One is by using seq command and another is by specifying range...
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019

How to Update Ubuntu Linux

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu

Hướng dẫn cài đặt Apache CloudStack

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader