Linux command
eval 命令
文件
Uses pipes, overwrite, or deletion. Check paths and options first.
命令示例
Execute dynamically
cmd="ls -la"; eval "$cmd"
Expand variables
var="PATH"; eval "echo \$$var"
Set a variable
key="myvar"; eval "$key=hello"; echo "$myvar"
Execute a command stored
cmd="ps aux | grep bash"; eval "$cmd"
Use eval with command substitution
eval "$(ssh-agent -s)"
说明
eval is a POSIX special shell builtin that concatenates its arguments separated by spaces, then reads and executes the resulting string as a shell command. This enables dynamic command construction and double expansion of variables. The command is useful when command strings are built programmatically or stored in variables. It allows variable indirection (accessing a variable whose name is in another variable). A common real-world use is initializing ssh-agent with `eval "$(ssh-agent -s)"`. If there are no arguments or only null arguments, eval returns exit status 0. Otherwise it returns the exit status of the executed command.
FAQ
What is the eval command used for?
eval is a POSIX special shell builtin that concatenates its arguments separated by spaces, then reads and executes the resulting string as a shell command. This enables dynamic command construction and double expansion of variables. The command is useful when command strings are built programmatically or stored in variables. It allows variable indirection (accessing a variable whose name is in another variable). A common real-world use is initializing ssh-agent with `eval "$(ssh-agent -s)"`. If there are no arguments or only null arguments, eval returns exit status 0. Otherwise it returns the exit status of the executed command.
How do I run a basic eval example?
Run `cmd="ls -la"; eval "$cmd"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
Where can I find more eval examples?
This page includes 5 examples for eval, plus related commands for nearby Linux tasks.