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

3
  • 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.) Commented Apr 19, 2017 at 4:03
  • @rici I don't understand what you mean by "don't quote the expansion." Commented Apr 19, 2017 at 4: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.) Commented Apr 19, 2017 at 4:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.