15.3 C
New York
Wednesday, October 5, 2022

Everything You Need to Know to Start Writing Bash Programs – MUO – MakeUseOf

You can use Bash scripts to automate all sorts of tasks. Get to grips with the fundamentals and begin your Bash scripting journey.
Bash scripts come in handy for automating tasks, and you’ll find they’re great for building simple command line applications. The Bash shell interprets Bash scripts, so you won’t need to install any dependencies to write and run them. Bash scripts are also portable since most Unix-based operating systems use the same shell interpreter.
Knowledge of Bash scripting is a must for every developer, especially if you work with Unix-based systems.
Bash variables are case-sensitive. To declare variables, use an equals sign (=) with the name on the left and value on the right:
The value this declaration assigns to STATE is a single word. If you need spaces in your value, use quotes around it:
You’ll need to use a dollar sign ($) prefix to reference variables in other variables or statements:
There are several ways you can print variables in Bash. You can use the echo command for basic output or the C-style printf command for string formatting.
After declaring the STATE variable, this script defines LOCATION by referencing STATE. If then uses echo to print the final value of the LOCATION variable.
The printf keyword allows you to use formatting verbs to output data. The string formatting verbs are similar to the ones in C and Go but with limited verbs.
Here’s an example of using a verb with the print keyword.
The printf function would substitute the STATE variable in the position of the %s verb, and the output would be “My Location is Lagos”.
You can make comments in Bash with the hash or pound (#) symbol. The shell automatically ignores comments.
There are no multi-line comments. Most IDEs and text editors allow you to comment with the Ctrl/Command + forward slash(/) shortcut. You should be able to use the shortcut to create multiple single-line comments.
Like many other programming languages, you can receive user input in Bash to make your programs/scripts more interactive. You can use the read command to request the user’s input.
In this case, the response variable will hold the user’s input on delivery.
The user input request will be on a new line in the example above.
You can add the -n flag to the echo print statement to retain the line where the user enters input.
Arrays in Bash are just like most languages. You can declare an array variable in Bash by specifying the elements in parentheses.
Accessing an array via reference to the variable name would fetch the first element. You can access the entire array by using the asterisk sign as the index.
You can also specify the index of the array to access a specific element. The index of an array starts at zero.
Bash provides conditionals for decision-making in programs.
Here’s the anatomy of an if-else statement in Bash. You’ll have to use the semi-colon to specify the end of the condition.
You must end every if statement with the fi keyword.
You can use case statements in your Bash programs using the case keyword. You’ll have to specify the pattern followed by ending parentheses before the statement.
You can define the default case using the asterisk (*) sign as the pattern. Case statements must end with the esac keyword.
Depending on your needs, you can use a while loop, range for-loop, or a C-style for loop for recurring operations.
Here’s an example of the C style for-loop. For-loops must end with the done keyword, and you must end the for statement with a semicolon followed by the do keyword.
The range for loop comes in handy for working with files and many other operations. You’ll need to use the in keyword with the range for-loop.
Here’s a simple infinite loop to demonstrate Bash while loops in action.
The -le in the condition statement is the binary operator for less than.
You don’t need keywords to declare functions in Bash. You can declare functions with the name and then parentheses before the function's body.
Functions can return variables in Bash. All you need is the return keyword.
The print_working_directory function returns the working directory of the file.
Bash isn’t the only language you can use to interact with your operating system's shell or build command-line applications. You can use many other languages like Go, Python, Ruby, and Rust.
Many operating systems have Python3 pre-installed, and Python is a prevalent language. If you need even more functionality than Bash scripts can offer, consider using Python.
Goodness is a mechanical engineering student and software developer passionate about cloud technologies and the Go programming language.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles