Henry
Henry

Reputation: 587

How to split string into parameters in bash?

For example, I want to run find but the parameter is stored in a varibile.

a='-name "build.sh"'
find . $a

It shows error: find: -name "build.sh": unknown primary or operator

I hope it runs like find . -name "build.sh", how to put the var as parameter`?

Thank you.

Upvotes: 0

Views: 224

Answers (1)

Joe Casadonte
Joe Casadonte

Reputation: 16859

Throw your code into a file and then use shellcheck on it, and it will lead you to this page:

https://github.com/koalaman/shellcheck/wiki/SC2089

Quotes/backslashes will be treated literally. Use an array.

Do it this way, instead:

a=(-name "build.sh")
find . "${a[@]}"

Upvotes: 1

Related Questions