How can we Run a Command Stored in a Variable?

In Linux, storing a command in variable increases the re-usability of that command. There are three ways to do this job using a bash script, an array, or an eval command. Let’s check and implement these methods one by one.

This post will demonstrate the methods to run a command stored in a variable; the content for this post is as follows:

Let’s start with the bash script.

Method 1: Run a Command Stored in the Variable Using Bash Script

Bash script enables the user to run the commands in sequence simultaneously. The user can store any command in the variable and use that variable to run the stored command. Let’s discuss it with examples.

Example 1: Running echo Command

We have stored an echo statement in a “command” variable which will print our message on the terminal:

#!/bin/bash
command=$(echo "welcome to itslinuxfoss")
echo "$command"

Let’s check the output of the above script in the terminal:

$ ./script.sh

The message stored in a variable is printed on the screen.

Example 2: Running pwd Command

Similarly, the user can also store the pwd command, which will print the current working directory. Run the following script in the terminal:

#!/bin/bash
command=$(pwd)
echo "$command"

Let’s check the output of the script:

$ ./script.sh

The current working directory “/home/itslinuxfoss” is printed on the screen.

Method 2: Run a Command Stored in the Variable Using an Array

The second method to run a command stored in a variable is an array. It is nothing but the concept of an array in programming languages. An array is used for creating multiple variables with specified indexes, and these variables can be used for storing the commands. Let’s understand this concept with examples.

Example 1: Executing cd Command

Let’s store the cd command in an array. To do so, execute the following command:

$ Command_Array=("cd" "Downloads")

The “cd” is stored at zero indexes and “Downloads” at one index. Execute the given command to get the result of the stored command in the variables.

$ "${Command_Array[@]}"

User has been moved to the Downloads directory.

Example 2: Checking Permissions of the File

Similarly, to store the command for checking the permissions of the file is obtained as follows:

$ Command_Array=("ls" "-l" "script.sh")

Let’s check the output by running the below command:

$ "${Command_Array[@]}"

The “script.sh” file has “-rwxrwxr-x” permission.

Method 3: Run a Command Stored in the Variable Using eval Command

The eval is a built-in utility to execute parameters as a command. It is the combination of the arguments stored in a single string and acts as an input to the shell. Users can use the eval command to store the command in the variable explained in the examples below.

Example 1: Executing hwinfo Command

To execute the command in a variable, type any name of the variable, then the “=” sign, and write down the command you want to execute. As in our case, we have stored an hwinfo command:

$ Command="hwinfo --cpu"

The command has been stored in the “Command” variable.

Let’s execute the stored command using eval command:

$ eval $Command

The hardware information of the CPU has been displayed.

Example 2: Showing Hidden Files

To show the hidden files using the eval command is obtained as follows:

$ Command="ls -a"

Once the command is stored, execute it using eval command:

$ eval $Command

    All the hidden files in the home directory have been displayed.

    These are the most efficient methods to run any command stored in a variable.

    Conclusion

    To run a command stored in a variable, use the bash script, an array, or an eval utility explained in the article. For the bash script, use any editor, such as nano, and add the script to it. For an array, store the command at indexes and run it or save the command in the variable using terminal parameters and run that variable using the eval command. This write-up has covered the methods to run a command stored in the variable.