0

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

Michael Martinez
  • 2,693
  • 1
  • 16
  • 19
  • Don't quote the expansion, and it will get word split (but it will split on every space, which might not be what you want either. And watch out for globs.) – rici Apr 19 '17 at 04:03
  • @rici I don't understand what you mean by "don't quote the expansion." – Michael Martinez Apr 19 '17 at 04:11
  • When you use $1. Presumably you use it somewhere. If you want to split it into positional parameters, you could use `set -- $1`. If you want to pass it to a different command as three words: `other_cmd $1`. As long as you don't ​quote the expansion (`"$1"`), it will get split. +But it's a suboptimal way to pass multiple arguments.) – rici Apr 19 '17 at 04:16

1 Answers1

1

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.

anon
  • 154
  • 9