Learn Bash Programming

29/12/2020
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!

ONET 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, ONET 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 escape quotes

Quoting is used to disable the special meaning of the special characters. There are many shell metacharacters which have...
29/12/2020

How to Search for your Files on the Linux Command Line

For a Linux desktop, a user can easily install an app to search their files and folders in the file system, but another...
29/12/2020

How to Execute Commands from Within a Shell Script

In bash, executing commands from a shell script can be a little intimidating at first and may require a leap of faith....
29/12/2020
Bài Viết

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

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024