0

I have a function like this

function test(){
   local param1=${1}
   local param2=${2}
   echo "Hey ${param1} ${param2}"
}

And then, in another part of the script I have this

echo ${command}
$(command)
echo "Completed"

The output of that execution is

test param1 param2
Completed

So, as you can see, the function test was not executed.

I was looking a way to do that if possible in my shell script.

4
  • command is a shell builtin which does nothing when called without any args (try running command at the command prompt); $( command ) says to run the (builtin) command in a subshell ... which still does nothing; to reference the variable command try $( $command ) ... not saying your code will work as expected but it should get you further along in the process; generally speaking ... naming variables, function and scripts with the same names as builtins and system executables (eg, test and command) isn't a recommended practice Commented Aug 2, 2022 at 16:21
  • Ok, I saw the response in this other post. Sorry. Commented Aug 2, 2022 at 16:22
  • BTW, re: function test() {, see wiki.bash-hackers.org/scripting/obsolete. Modern POSIX-compliant function declaration syntax is just test() {; 1980s ksh used function test { with no (); merging the two gives you the worst of both worlds, as your code is compatible with neither legacy ksh nor baseline POSIX. Commented Sep 11, 2022 at 1:29
  • Also, using test as your function name is probably best avoided, as it shadows the built-in test command. Commented Sep 11, 2022 at 1:30

1 Answer 1

0

The good practice for this is described below:

command=(test a b)

echo ${command}
"${command[@]}"
echo "Completed"

The above code will use an array to store the command and the parameters, and it will call the function appropriately.

To specifically fix the expected behavior of your code, you could call the function using ${command} or $command, which is different from $(command). But the preferred practice, and more secure way of doing that is to follow the previous suggestion.

#!/bin/bash

function test(){
   local param1=${1}
   local param2=${2}
   echo "Hey ${param1} ${param2}"
}


command="test a b"

echo ${command}
${command}
echo "Completed"
Sign up to request clarification or add additional context in comments.

7 Comments

This works only for very limited cases; one quickly runs into the bugs described in BashFAQ #50. Far safer to store a multi-word command in an array rather than a string.
That is, command=( test a b ), then "${command[@]}" to run it. That way you can do things like command=( printf '%s\n' "first line" "second line" ), and "${command[@]}" correctly prints two lines. Try it yourself if you want to see what that does with the string approach; it's not pretty.
(...also, please @-notify me once you've fixed up this answer so it's no longer hitting BashFAQ #50's bugs, and I'll gladly reverse the downvote).
@CharlesDuffy Hello, I've added more information regarding your observations, and I believe it is a more complete answer now.
Downvote reversed (someone else added another one, but that I can't fix). I do suggest removing the buggy branch of the answer in favor of the robust one, or at least putting the good-practices form first.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.