Bash eval Command Tutorial

Linux bash provides the eval command in order to run a specified string or text as a command. The provided text or string is read and concatenated together into a single command. Then this command is executed in the bash shell. The exit status of the executed command is returned as the value of eval execution. If no text or string is provided the eval returns 0.

eval Command Syntax

The bash eval command has very simple syntax. It only accepts single or multiple strings and concatenate into single string to execute.

eval STRING1 STRING2 ...
  • STRING1 is the string which is concatenated and executed.
  • STRING1 is the string which is concatenated and executed. This is optional.

Evalute Specified Command Text

The eval command is used to evaluate specified text as a command in the bash shell. In the following example, we will provide the ls text to the eval command. The ls will be evaluated in the bash shell which will list current working directory files and folders.

eval ls
Evalute Specified Command Text

Evalute Specified Command Text with Arguments and Parameters

The eval command may accept multiple parameters which are joined into single text and executed in bash shell. This can be used in order to providews command and parameters like below. In the following example we will execute the ls command with the -l and -h options.

eval ls -l -h

Evaluate Multiple Text

Providing the eval parameters as text by using single or double quoto is very important. The command and its parameters can be provided in quotos as multiple text.

eval "ls" "-l" "-h"

Evalute Bash Variable

Some bash scripts created to be dynamic as they store the commands in variables. In order to accomplish tasks, these variables’ contents should be executed in the bash shell. The eval command can be used to execute a bash variable content in the bash shell. In the following example, we will create a variable named $command and set some text to it which will be executed as a command.

command="ls -lh"

eval $command

Leave a Comment