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.
Key Takeaways
- Learning the process of executing commands stored in Bash variables.
- Knowing the process of executing a lengthy command with options and arguments using a variable array.
Free Downloads
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: Executing Command Stored in a Variable
A programmer can execute a command stored in a variable. Here, I have developed a script where I have executed the pwd command stored inside the cur_dir variable. To know the details, follow the below script.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano script1.sh
- nano: Opens the nano text editor.
- script1.sh: Bash script name.
❸ Copy the script mentioned below:
#!/bin/bash
#setting the value of pwd command to cur_dir variable
cur_dir=$(pwd)
#printing the value
echo "Current Working Directory: $cur_dir"
#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash.\. Then the cur_dir variable is set where the value is the output of the pwd command. Finally, the value of the cur_dir variable is printed on the terminal.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❺ Use the following command to make the file executable:
chmod u+x script1.sh
- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- script1.sh: File that you want to make executable.
❻ Run the script by the following command:
./script1.sh
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.
You can follow the steps of case 01, to save and make the file executable.
Script (cur_dir2.sh) >
#!/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.
Run the script by the following command:
./cur_dir2.sh
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.