🧠 5. Bash Internals & Shell Behavior
Estimated reading: 3 minutes 39 views

🧨 Bash: Execute Commands – Understanding exec and eval in Bash Scripts


🧲 Introduction to exec and eval in Bash – Dynamic and Direct Command Execution

Bash provides two advanced tools for executing commands: exec and eval. These commands serve very different purposes:

  • exec replaces the current shell with another command.
  • eval constructs and runs a command dynamically.

Understanding how to use them effectively helps you build dynamic, efficient, and secure Bash scripts for advanced automation, command construction, and performance tuning.


🎯 In this article, you’ll learn:

  • How exec replaces the shell process
  • How eval evaluates dynamic command strings
  • Key differences and use cases
  • Real-world examples for redirection, SSH, and command wrappers

⚑ What Does exec Do in Bash?

The exec command replaces the current shell process with the specified command. The shell doesn’t continue after exec; the new command takes over.

πŸ§ͺ Example 1: Replace Shell with top

#!/bin/bash
exec top
echo "This will never run."

βœ… Output:

(top runs in place of the script β€” echo is never executed)

🧠 exec doesn’t spawn a new process. It replaces the shell itself.


πŸ§ͺ Example 2: Redirect All Output to a Log File

#!/bin/bash
exec > output.log 2>&1
echo "This is logged to the file."

βœ… Creates output.log with:

This is logged to the file.

πŸ”§ exec is also used to change file descriptors β€” very useful in logging or daemon scripts.


πŸ”„ What Does eval Do in Bash?

The eval command evaluates a constructed command string and executes it as if it were typed at the shell.

πŸ§ͺ Example 1: Construct and Run a Command

cmd="ls -l"
eval $cmd

βœ… Output:

(total and file listing)

πŸ” Without eval, Bash would just treat $cmd as a string, not execute it.


πŸ§ͺ Example 2: Dynamic Variable Expansion

varname="USER"
eval echo \$$varname

βœ… Output:

(your current username)

βœ… eval interprets \$USER and runs echo $USER.


πŸ” Warning: eval Can Be Dangerous

Using eval on untrusted input is a major security risk. It executes arbitrary code, which can be exploited if input is manipulated.

❌ Risky Example:

user_input='rm -rf /'
eval $user_input

🚨 This would run destructive commands if misused.


πŸ” Summary Table – exec vs eval

Featureexeceval
Replaces Shell?βœ… Yes❌ No
PurposeReplace shell or redirect file descriptorsRun dynamically constructed commands
Typical UseRedirection, terminal replacementDynamic variables, alias expansion
Continues Script?❌ No (terminates shell)βœ… Yes
Security RiskLow⚠️ High (if used with untrusted input)

πŸ“Œ Summary – Bash exec and eval

The exec and eval commands give you deep control over how scripts execute commands and manage process flows. Use exec for efficiency and resource control. Use eval for dynamic evaluationβ€”but only with trusted input.

πŸ” Key Takeaways:

  • exec replaces the shell or redirects I/O
  • eval builds and runs commands from strings
  • exec is safer and used for logging or daemons
  • eval is powerful but should be used with extreme care

βš™οΈ Real-world Uses:

  • exec to redirect output in logs or cron jobs
  • eval to dynamically invoke commands in automation tools
  • Use exec to launch programs from scripts without leaving background shells open

❓ FAQ – Bash exec and eval


❓ What happens after an exec command?
βœ… The current shell is replaced by the new command. Any code after exec does not run.


❓ Is eval safe to use in Bash?
βœ… Only with fully trusted input. Avoid using eval with user dataβ€”it’s a common attack vector.


❓ Can I use exec for logging all script output?
βœ… Yes:

exec > logfile.txt 2>&1

This redirects all future output in the script to the log file.


❓ What’s a safe alternative to eval?
βœ… Use arrays or case statements instead of building commands as strings.


❓ Can I use exec to run a script inside another script?
βœ… Yes. It replaces the parent script:

exec ./otherscript.sh

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🟒 Bash: Execute Commands (exec, eval)

Or Copy Link

CONTENTS
Scroll to Top