2023-03-05    Share on: Twitter | Facebook | HackerNews | Reddit

Eval in Bash Script With Arguments Implemented as Functions

minimal example of how you can use functions and eval in a bash script:

#!/bin/bash

hello () {
  echo "Hello, $1!"
}

eval "$1 World"

This script defines a function called hello that takes one argument and prints a greeting message with that argument. Then, it uses eval to execute the first argument passed to the script followed by the string "World" as the argument to the function.

For example, if you save this script as greeting.sh and run ./greeting.sh hello, it will execute the hello function with "World" as its argument, and the output will be:

Hello, World!

You can modify this example to fit your needs by defining your own functions and passing arguments to them through eval.



Tags:  bash eval args