What Does Set -- “$@” "$I" Mean in Bash?

The set command in the following loop is confusing to me.

for i in "$@"
do 
  set -- "$@" "$i" # what does it mean?
done

I can understand $@ is all the positional parameters, and $i is one of the positional parameters. However, I can't figure out what

set -- "$@" "$i" 

means.

0

1 Answer

It's appending the value of $i onto the end of the positional parameters. Not sure why one would want to do it, but it's basically a verbose way of doubling the parameters. It has the same affect as

$ set -- a b c
$ echo "$@"
a b c
$ set -- "$@" "$@"
echo "$@"
a b c a b c
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest