Home > Blockchain >  How to split string into parameters in bash?
How to split string into parameters in bash?

Time:04-01

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.

CodePudding user response:

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[@]}"
  •  Tags:  
  • bash
  • Related