'How to re-write this bash method without using "eval" [duplicate]

Here is a script newbie question. I am invoking a script as follows (not space in hello world):

$ ALL_ARGS="-t 30 -h 'hello world'" ./test.sh

Inside test.sh I would like to pass the parameters in ALL_ARGS as 4 separate parameters, but without using the 'eval' call.

#!/bin/bash

#my_function should receive 4 parameters
#

my_function () { 
    printf "'%s' " "$1"
    printf "'%s' " "$2"
    printf "'%s' " "$3"
    printf "'%s' " "$4"
    echo ""
}

echo "Desired Output, but without using eval"
# my_function '-t' '30' '-h' 'hello world'
eval "my_function $(echo $ALL_ARGS)"

As you can see, I am calling my_function from inside my script and passing in the value of $ALL_ARGS. The problem is the "space" in the $ALL_ARGS. The following attempts have failed, for various reasons:

my_function $(echo $ALL_ARGS)
my_function $(echo "$ALL_ARGS")
my_function "$(echo "$ALL_ARGS")"
my_function "$(echo $ALL_ARGS)"
my_function "$ALL_ARGS"
my_function $ALL_ARGS

In essence, I want the call to look something like this when it executes in the script:

my_function -t 30 -h 'hello world'

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source