Michael Martinez
Michael Martinez

Reputation: 2853

how to tell a bash script to parse a single command-line argument that contains spaces?

I have a bash script that accepts multiple command line arguments ($1, $2, ... etc). The program that's launching the script is, for reasons I won't get into here, passing the command line arguments as one string which the bash script interprets as $1. This one string contains spaces between the desired arguments, but bash is still interpreting it as a solitary command line argument. Is there a way to tell bash to parse it's command line argument using a space as delimiter?

For example, if argstring = 50 graphite downtick I want bash to see $1=50 $2=graphite $3=downtick, instead of $1=50 graphite downtick

Upvotes: 0

Views: 119

Answers (1)

anon
anon

Reputation: 154

Just add this line at the top of your program:

set -- $1

More info about set in the bash reference manual and another example of its usage in this Stack Overflow answer. Basically, it can be used to replace the arguments being passed into your script.

Upvotes: 1

Related Questions