my main question how split strings on command line parameters using terminal command in linux? example on command line:
./my program hello world "10 20 30"
the parameters set as:
$1 = hello
$2 = world
$3 = 10 20 30
but want:
$1 = hello
$2 = world
$3 = 10
$4 = 20
$5 = 30
how can correctly?
you can reset positional parameters $@
using set builtin. if not double-quote $@
, shell word-split producing behavior desire:
$ cat my_program.sh #! /bin/sh i=1 param; echo "$i = $param"; i=$(( $i + 1 )); done set -- $@ echo "reset \$@ word-split params" i=1 param; echo "$i = $param"; i=$(( $i + 1 )); done $ sh ./my_program.sh foo bar "baz buz" 1 = foo 2 = bar 3 = baz buz reset $@ word-split params 1 = foo 2 = bar 3 = baz 4 = buz
as aside, find mildly surprising want this. many shell programmers frustrated shell's easy, accidental word-splitting — "john", "smith" when wanted preserve "john smith" — seems requirement here.