2 Cases to Execute Command Stored in Bash Variable

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Bash programmers can incorporate various commands into the bash script. The commands help programmers to achieve the target task. Sometimes programmers need to use or execute command stored in bash variable. In this tutorial, I discussed ways to execute a command stored in a variable.

2 Cases to Execute Command Stored in Variables Using Bash Scripts

You can store commands in the Bash variable and execute them later. Here, I have given two such cases. In the first case, I will execute a command stored in a variable, and then in the second case, run a command stored in a variable array. I hope these simple examples of executing commands stored in variables will be helpful for you.

Case 01: Execute a Command Stored in a Variable

To execute a command stored in a variable, I have developed a script where I have executed the pwd command stored inside the cur_dir variable. To know the details, follow the script:

#!/bin/bash

#setting the value of pwd command to cur_dir variable
cur_dir=$(pwd)

#printing the value
echo "Current Working Directory: $cur_dir"
EXPLANATION

The cur_dir variable is set where the value is the output of the pwd command. Then the value of the cur_dir variable is printed on the terminal.

The Bash script has executed the pwd command stored inside the cur_dir variable.The image shows that the script has executed the pwd command stored inside the cur_dir variable.

Case 02: Run a Command Stored in the Variable Using an Array

You can store a command in a variable array and type the variable name to execute a long command with options and arguments whenever necessary. Here I have stored the ls -l command along with a parameter inside the command array. To know more, follow the below script:

#!/bin/bash

#assigning ls -l command on the Command_Array variable
Command_Array=("ls" "-l" "varchk.sh")

#Commad_Array has been executed
"${Command_Array[@]}"
EXPLANATION

At first, the "ls" "-l" "varchk.sh" command has been stored in the Command_Array. Then the value of Command_Array has been printed, which is basically a command to print the details of the varchk.sh file.

The Bash script has successfully executed the “ls” command with “-l” option and "varchk.sh" argument.The image shows that the Bash script has successfully executed the “ls” command with “-l” option and “varchk.sh” argument.

Conclusion

In this article, I explored scenarios to execute a command stored in a variable. Programmers use variables on the bash script to solve the task assigned to them efficiently. Executing a command stored in a Bash variable is essential in cases where you want to just type the name of a variable, and it will execute a whole command with or without an option. In such circumstances, by going through this article, I hope you will be productive enough to execute the command stored in the Bash variables.

People Also Ask

How do you use a variable in a Bash command?

To use a variable in a Bash command, you need to put the $ symbol before the variable name to refer to the variable and print it out. But whenever a user need to modify its value, using the $ symbol is not mandatory.

What are the rules for variable names in Bash?

Variable names can consist of a sequence of alphanumeric characters and underscores. You can start a variable name with an underscore or alphanumeric letter, not with the number.

How to read a variable in bash?

To read a variable in Bash, you can use the read command. It takes the user input and splits the string into fields, assigning each new word to an argument. If there are fewer variables than words, read stores the remaining terms into the final variable.

What is the purpose of the variable?

Variables store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.


Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

4.6/5 - (5 votes)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment