Variables are used to store different types of values ​​during program execution. There are two types of variables in Bash scripting: global and local.

Global variables can be used by all Bash scripts on your system, while local variables can only be used within the script (or shell) in which they are defined.

Global variables are generally provided by default on the system and are mainly environment and configuration variables. Local variables, on the other hand, are user-defined and have arbitrary uses.

bash local variables

To create a variable, you need to assign a value to your variable name. Bash is an untyped language, so you don’t need to indicate the data type when defining your variables.

Like many other programming languages, Bash uses the assignment operator = to assign a value to a variable. It is important to note that there should not be any spaces on either side of the assignment operator. Otherwise, you will get a compilation error.

Another important point to note: Bash does not allow you to first define a variable and later assign a value to it. You must assign a value to the variable when you create it.

Sometimes, you may need to specify a string that contains a space in your variable. In such case, enclose the string in quotes.

Note the use of single quotes. These quotes are also called “strong quotes” because they specify the value exactly as it is written, without regard to any special characters.

In the example above, you could also use double quotes (“weak quotes”), although that doesn’t mean they can always be used interchangeably. This is because double quotes will replace special characters (such as those with $) instead of being interpreted literally.

If you want to assign command-line output to your variable, use backquotes (“). They will treat the string appended to them as a terminal command and return its result.

parameter expansion in bash

Parameter expansion simply refers to accessing the value of a variable. In its simplest form, it uses the special character $ followed by the variable name (with no space in between):

You can also use the syntax ${variableName} to access the value of a variable. This form is more appropriate when confusion may arise regarding variable names.

If you omit the curly brackets, ${m}ical will be interpreted as a compound variable (which doesn’t exist). This use of curly brackets with variables is known as “substitution”.

global variables

As mentioned earlier, there are some built-in variables in your Linux system that can be accessed in all your scripts (or shells). These variables are accessed using the same syntax as local variables.

Most of these variables are in capital letters. However, there are some single characters that are not also alphanumeric characters.

Loops in Bash Scripting

Now you know what variables are, how to assign them, and how to perform basic bash logic using them.

Loops enable you to iterate through multiple statements. Bash accommodates for loops and while loops with simple syntax for all your looping needs.

If you’re looking to master the art of Bash development, then the for loop should be next on your list.

Looping is an inherent art, which can simplify your work and help you automate repetitive tasks with relative ease.

Imagine a situation in which you need to update a range of numbers or text, and instead of doing it manually, you have the system do it for you. This is the power of looping and the benefits it brings to the table for you.

Loops, as a function, are available in almost every programming language; Linux’s Bash is no exception to this rule.

You can define the number of iterations in the first line. This way, you’ll be mentioning the starting price and the ending price.

The number of iterations is determined by the values ​​you specify, while the code following the do statement is the resulting loop value.

Leave a Reply

Your email address will not be published. Required fields are marked *