Analyse the action of 'set -- $args'


set updates the positional parameters of the script.

vim test.sh
#! /bin/bash
echo "$*"
set -- $1 baz
echo "$*"

Execute the test.sh with three arguments foo bar foobar.

bash  test.sh  foo bar foobar
foo bar foobar
foo baz

Why the result is not as below?

bash  test.sh  foo bar foobar
foo bar foobar
foo baz foobar
---------------Answer---------------

$1 is, in your example, foo. Therefore you effectively do a

set -- foo baz

This throws away the positional parameters we have so far (your foobar is gone) and creates them from fresh:

$1 -> foo

$2 -> baz


Previous : Why this erlang prog with type signature can compile?
Next : Return only single column from Laravel hasOne relation