Difference between "set $<var>" and "set -- $<var>"

Hi pros,

Could anyone tell me the actual difference between setting the positional parameters from the variable using the following commands?

$ var="This is my question"
$ set $var
$ echo $1  --> 'This'
$ echo $2  --> 'is'
  ....
  ....

and

$ var="This is my question"
$ set -- $var
$ echo $1  --> 'This'
$ echo $2  --> 'is'
  ....
  ....

In the example you show, there is no difference but look at this one...

$ set one two
$ echo $1
one
$ echo $2
two
$ set -o emacs
$ echo $1
one
$ echo $2
two
$ set -- -o emacs
$ echo $1
-o
$ echo $2
emacs

Big difference between
set -o emacs
and
set -- -o emacs
emacs
$

Then, how about "set - $<var>" ?? (single hypen setting the positional parameters)
for eg: set - mydir/file.cpp --> what is the difference with that of 'set --' ??

It took you 6 months to post a followup question? Oh well...

$ set one two
 $ set -vx
$ echo hello
echo hello
+ echo hello
hello
$ echo 1 = $1 and 2 = $2
echo 1 = $1 and 2 = $2
+ echo 1 = one and 2 = two
1 = one and 2 = two
$ set -- -o emacs
set -- -o emacs
+ set -- -o emacs
$ echo 1 = $1 and 2 = $2
echo 1 = $1 and 2 = $2
+ echo 1 = -o and 2 = emacs
1 = -o and 2 = emacs
$ echo hello
echo hello
+ echo hello
hello

So, ok I set the x and v option and I'm getting a bunch of output. The "set -- -o emacs" had the usual effect that we discussed above. Now I'll try a "set - -o vi" and see and happens...

$ set - -o vi
set - -o vi
+ set - -o vi
$ echo 1 = $1 and 2 = $2
1 = -o and 2 = vi
$ echo hello
hello
$

The "set -" had the additional effect of turning off both -v and -x. The "set -" was invented first and at the time, x and v were the only options available and so "set -" turned off all options. Modern thinking frowns on syntax like "set -" and now "set --" is preferred. Modern thinking also frowns on a single item like "set -" having that many effects. Now we have "set +v" to turn off an option. And while "set -v" and "set +v" may seem a little backwards, at least it's now one effect per syntax item and that's progress.

So "set -" is a historical oddity and should be avoided.

I'll check back next April in case you have a further question. :slight_smile: