What is the eval command in Linux?

Overview

On a Unix or Linux system, the eval command is used to run the arguments as a shell command.

When you have a Unix or Linux command saved in a variable and wish to run that command, the eval command is useful. The command evaluates the argument first, then executes the command it contains.

Syntax

eval [arg]...

The arguments are read and combined into a single command. The shell then reads and executes this command, and the result is returned as the value of eval.

Example Code

The command below executes the echo command and prints hi.

eval echo hi

The command below executes the ls command stored in the variable COMM.

Refer to What is the ls command in Linux? to learn more about ls command.

COMM="ls"
eval $COMM
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources