eval
A shell builtin that joins its arguments into one string and runs that string as a command, as if you had typed it.
Any text from outside that reaches eval runs as code with the script's permissions.
In more detail
The text is expanded a second time, so any quote, semicolon or $( ) that arrives inside data becomes live code, and eval on untrusted input is command injection. A common safe use is running trusted setup output, such as eval "$(brew shellenv)", which sets PATH and other variables in the current shell. Only eval text you trust, because it runs with your full permissions.
Related terms
- command injection — An attack where input meant as data, such as a file name or form field, gets run as commands because a program built code from it.
- indirect expansion — The bash form ${!name}, which reads the value of the variable whose name is stored in name.
- nameref — A bash 4.3 variable declared with declare -n or local -n that acts as another name for a variable named by its value.
- shell array — A variable that holds a numbered list of separate values, written in bash as names=(one "two words" three).
- shell profile — A startup file the shell runs when it opens, used to set PATH and other settings for every session.
- Homebrew — A popular package manager for macOS and Linux, run with the brew command, which installs software into its own directory.
Taught in
- eval, And What To Use Instead — Bash Scripting & Command Line Automation
- Security Testing: Red, Blue And Purple — ISC2 Certified in Cybersecurity (CC) Exam Prep
- Installing Python On A Mac — Hands-on Python: Complete Video Course & Book
- The Exception Family Tree — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- What Changed Last Night? Integrity, Scans, Benchmarks And Secure Deletion — CompTIA Linux+ (XK0-006) Exam Prep
References
- Bash Reference Manual: Bourne Shell Builtins (eval)
- CWE-95: Eval Injection
- zsh manual: shell builtin commands
- Homebrew installation