In our previous article, we discussed the differences between local and global Bash variables (you can read it here). Building upon that understanding, today, we will dive deeper into the world of Bash scripting, focusing on Bash variables. We will explore the rules of defining variables, operations we can perform on them, and how they interact with your scripts.

Advertisement

What are Bash Variables?

Bash variables are a type of data reserved in memory. These variables can hold information like text strings, numbers, or any other type of data. Variables are essential in Bash scripts as they allow for the creation of dynamic programs.

In the context of Bash scripting, a variable is a symbol or name that stands for a value. The name assigned to a variable is also called an identifier.

How to Define a Bash Variable

In Bash scripting, a variable is assigned a value using the “=” operator. The syntax for defining a variable is:

A few rules you should keep in mind while naming your variables in Bash:

  1. The variable name must begin with an alphabetical character or an underscore.
  2. The variable name cannot have spaces.
  3. The variable name should not contain any special characters except the underscore.
  4. Bash is case-sensitive. Therefore, “VAR” and “var” would be two distinct variables.

Here’s an example of variable declaration and assignment:

In this script, the variable “name” is assigned the value “John Doe”. The echo command is then used to print the value of “name” on the console. The dollar sign ($) is used to reference the value of a variable.

Arithmetic Operations on Bash Variables

Bash allows arithmetic operations to be performed on variables. The `expr` command or double parentheses `(())` can be used to perform basic arithmetic operations:

In this example, two variables “a” and “b” are declared with the values 10 and 20, respectively. The sum of these variables is then calculated using two methods: the expr command and the double parentheses syntax. Both will output “Sum: 30”.

Bash Environment Variables

In addition to user-defined variables, Bash also provides a set of predefined variables called environment variables. These are used by the shell to keep track of certain system attributes. Examples of these are:

  1. `$HOME`: the current user’s home directory
  2. `$PATH`: a list of directories in which the shell looks for commands
  3. `$USER`: the name of the current user
  4. `$PWD`: the current working directory

You can display the value of an environment variable using the echo command:

This will print the path of the current user’s home directory.

String Operations on Bash Variables

Bash provides several operations to manipulate strings. Here’s an example:

In the script above, we calculate the length of the string with `${#name}`, slice the string with `${name:0:4}`, and replace “Bash” with “Shell” in the string with `${name/Bash/Shell}`.

Conclusion

Bash variables allow you to store and manipulate data in your scripts, making them dynamic and flexible. From simple string manipulations to complex arithmetic operations, Bash variables offer a wide range of possibilities. Understanding these concepts is key to becoming proficient in Bash scripting.

Stay tuned for our next article, where we will cover the exciting topic of “Bash Arrays”. Happy scripting!

Share.
Leave A Reply


Exit mobile version