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"
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 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[@]}"
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 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
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners