1

I have a bash script that takes some arguments and uses some of them to loop over some action while some others (those of the form 'a=b') are passed to that action. Essentially, it looks like

args=
numbers=
for arg in "$@"; do
    if [[ "$arg" == *=* ]]; then
        args="$args $arg"
    else
        numbers="$numbers $arg"
    fi
done
for number in $numbers; do
    some_action $number $args
done

So, for example, if I call it as script 1 2 3 a=b c=d, it runs

some_action 1 a=b c=d
some_action 2 a=b c=d
some_action 3 a=b c=d

The problem is with whitespaces. Sometimes I need to pass arguments like resume='5 8'. Once they are stored in $args, the distinction between spaces and arguments is lost, a call to script 1 2 resume='5 8' results in

some_action 1 resume=5 8
some_action 2 resume=5 8

so 8 is a separate argument. I tried to create the args string with newlines, using IFS='\n', but this somehow resulted in superfluous calls to some_action. Does anyone have an idea how to fix this?

  • is calling: script 1 2 resume="'5 8'" an acceptable usage method? That's double quotes on the outside, with single quotes on the inside. That provides the desired output internally – Gravy Mar 09 '16 at 16:15

0 Answers0