When using the
shift built-in, have noted this interesting behavior
which I have been unable to find documentation for.
Update: the answer was right in front of me, if
shift is called with a value greater than arguments available in
$1 - $x then it will return with a non zero status - hence the script will halt (since I'm using
Using the script
test.sh, executed using arguments:
./test.sh apple orange banana apple orange banana banana red blue
- Three arguments echoed.
- Shift two away.
- One remains as
And then without arguments:
./test.sh # no output
$1 - $3 don't exist, so no output or shift but
$variable2 are now broken!?!
Why? I'm not sure. Appreciate any comments!
But what this does show, it's safer to check the number of positional arguments available before calling
[[ $# -ge 2 ]] && shift 2