Greenwebpage Community Blog Tutorials How to Work With Bash Variables
Tutorials

How to Work With Bash Variables

In each operating system, there is an interface that helps to interact with the programs and manage the files in the computer. In Windows, this interface is termed CLI (Command Line Interface), and in Linux, there are multiple interfaces but the most common is termed Bash.

In this article, the below contents will be discussed in detail:

  • What is Bash?
  • Difference between Bash and Shell
  • What are Bash Variables?
  • Rules for Bash Variables
  • Types of Bash Variables
  • Working With Bash Variables
  • Working With Special Bash Variables
  • Conclusion

1. What is Bash?

Bash is basically a Unix shell and is mostly used as a default login shell for Linux apart from other login shells. It is commonly used to test the software, compile the code, and debug the designed code. Moreover, Bash is also used by network engineers to test, configure, and optimize the organizational network.

2. Difference Between Bash and Shell

The main difference between Shell and Bash is that Shell is a program that is responsible for providing CL I(Command Line Interface). Whereas, Bash is a special Shell type and is specifically used with Unix and Linux.

3. What are Bash Variables?

Variables represent numeric and string values. The Bash variables are used to store the reference values. These variables use a $ sign at the start of the variable name such as $myuser.

4. Rules for Bash Variables

Variables are a part of every programming language. Similarly, the variables in the Bash consist of a number, a character, and a string of characters. There is a need to follow certain rules when working with Bash variables.

These rules are listed below:

  1. To read or print the variable use the $ sign first.
  2. The name of the variable is considered case-sensitive therefore C and c are considered entirely different.
  3. The variable can be in Lowercase or Uppercase or even a mixture of both.
  4. Omit the $ sign, when you assign a value for the variable.
  5. There should not be any space before or after an = sign when assigning a value to the Bash variable like variable1 =va1(This convention is wrong).
  6. The variable can be declared as alphanumeric or with an underscore(_).

5. Types of Bash Variables

The bash variables can be divided into two types, the first one is the system-defined variables and the second one is the user-defined variables. Both types are defined below.

5.1 System-Defined Bash Variables

These variables are also called environment variables and are defined by the system already. These variables are used by the Bash script since these variables are defined by the system.

The system-defined Bash variables are listed below:

  • SHELL: The name of the SHELL currently used

  • PWD: Displays the directory which is currently working

  • LOGNAME: Displays the user who is currently logged in

  • HOME: Display the user’s home directory

  • USER: Displays the current user

  • PATH: Shows the Bash directories to search for the commands

Apart from the variables mentioned earlier, some other system-defined variables are used according to the need.

5.2 User-Defined Bash Variables

As per the name, these variables are also termed local variables and are defined by the user only. These variables work only on the specified script.

For example, A variable in Bash can be created like myuser=user12. On passing the $echo command the value for myuser is printed as user12.

6. Working With Bash Variables

The variables in Bash can be defined and used in Shell as well as in Bash script. Now, we will look into the details regarding variables in the Bash script:

  • In the first step, create a Bash script file of the text editor as per your preference.
  • Use the variable of your choice such as $SHELL to look for the shell that is currently in use.
  • In the last step, run the script accordingly.
  • You can either use the system-defined variables or the user-defined variables as it depends on the requirement.

7. Working With Special Bash Variables

There are certain special Bash variables that are also used which are listed below.

  • $$: It displays the current shell’s process ID.
  • $0: It displays the current script’s file name.
  • $-: It displays the flags that are currently in use by Bash.
  • $@: It stores the argument in the form of an array.
  • $!: It displays the ID for the last background job.

Apart from the above-mentioned special characters, there are some other special characters also which can be used according to the requirement.

This concludes the working of the Variables in Bash.

Conclusion

The Variables in Bash are implemented as per the requirements. You can either use the system-defined variables or the variables created on your own. In this article, we have elaborated on the details regarding what Bash variables are, and how to work with the Bash variables and special Bash variables.

Exit mobile version