0

I have a script and am passing a command that I want to then run within the script.

Passing the function like:

bash ./myscript.sh 'yarn test'

I can run the command if I first save it as a variable and then run it like:

FUNC=$1

$FUNC

But can't seem to figure out how to just run it without first saving it.

EDIT:

I should have been more specific with my example, my code more accurately looks like: (using KamilCuk's reply suggestion)

run_command () {
    "$@"
}

run_command

Having the value inside of a function in the script won't run the passed command, having it outside as simply "$@" will run it.

Is there something different I would need to do to have it run within the function?

7
  • Why don't you write simply $1 as a command? BTW, you are not passing a function, but the name of a function, which is something different. Functions are not first-class objects in bash, and can't be passed around. Commented Apr 8, 2021 at 13:36
  • @user1934428 Edited OP to be more clear about what I'm passing. Also have tried simply using $1 and it doesn't run it. Commented Apr 8, 2021 at 13:37
  • 1
    works here I can run the function it's not a function, it's a command. And you should rather prefer passing bash ./myscript.sh yarn test and then do "$@" to properly handle corner cases, like spaces or * characters in arguments. Commented Apr 8, 2021 at 13:43
  • Start ./myscript.sh yarn test and run "$@". Otherwise you'll have serious problems when trying to pass arguments with quotes and spaces. Which, now that I read, @KamilCuk already told you. :) Commented Apr 8, 2021 at 13:51
  • Apologies, I wasn't accurate with my example in the OP. Have updated the example to show that I'm trying to run the passed command within a function in the script. Commented Apr 8, 2021 at 13:52

1 Answer 1

2

Maybe there's something wrong in your script, or yarn can't be found?

Here's an [updated] trivial script and it works ...

#!/bin/sh

run_command() {
    "$@"
}

run_command $@

and when I run it it does run the passed in parameter.

# /var/tmp/myscript.sh "echo Hi"
Hi

You can use the -x option when launching to get a debug trace .. e.g.

# sh -x /var/tmp/myscript.sh "echo Hi"
L-MacBook:TEST rohan$ sh -x /var/tmp/myscript.sh "echo Hi"
+ run_command echo Hi
+ echo Hi
Hi
Sign up to request clarification or add additional context in comments.

7 Comments

Buggy, though. See BashFAQ #50
Apologies, I wasn't accurate with my example in the OP. Have updated the example to show that I'm trying to run the passed command within a function in the script.
Yes that updated answer works, thank you. Guessing there is therefore no way to run it within the run_command function straight from it being passed in the script? It has to explicitly be passed to that function too?
@chivs890 - the "$@" - that's all the args passed to run_command ... each function gets it's own set of $1/$2/etc... so no, not unless your pass or save.
change run_command $@ to run_command "$@"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.