2 Cases to Execute Command Stored in Bash Variable

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.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano script1.sh
EXPLANATION
  • 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"
EXPLANATION

#! /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
EXPLANATION
  • 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 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.

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[@]}"
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.

Run the script by the following command:

./cur_dir2.sh

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?
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?
The read command 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.
Rate this post
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