Skip to main content
Link to Q about restoring IFS
Source Link
Gordon Davisson
  • 5.2k
  • 1
  • 21
  • 19

EDIT: See this question for more options & discussion of how to save & restore the value of IFS.

BTW2, in most situations you don't want the arguments either merged or split, so what you want is "$@". Unlike $*, $@ doesn't merge the arguments, and with double-quotes it doesn't split them either (or expand them as filename wildcards) -- it just passes them straight through. But don't use args="$@", because that actually does merge them like "$*" (or maybe with " " instead of the first char of IFS, depending on the shell) (it has to merge them to save them in a plain variable). Use args=("$@") instead, to save them as an array.

BTW2, in most situations you don't want the arguments either merged or split, so what you want is "$@". Unlike $*, $@ doesn't merge the arguments, and with double-quotes it doesn't split them either (or expand them as filename wildcards) -- it just passes them straight through. But don't use args="$@", because that actually does merge them like "$*" (or maybe with " " instead of the first char of IFS, depending on the shell) (it has to merge them to save them in a plain variable). Use args=("$@") instead, to save them as an array.

EDIT: See this question for more options & discussion of how to save & restore the value of IFS.

BTW2, in most situations you don't want the arguments either merged or split, so what you want is "$@". Unlike $*, $@ doesn't merge the arguments, and with double-quotes it doesn't split them either (or expand them as filename wildcards) -- it just passes them straight through. But don't use args="$@", because that actually does merge them like "$*" (or maybe with " " instead of the first char of IFS, depending on the shell) (it has to merge them to save them in a plain variable). Use args=("$@") instead, to save them as an array.

Added clarification about args="$@"
Source Link
Gordon Davisson
  • 5.2k
  • 1
  • 21
  • 19

BTW2, in most situations you don't want the arguments either merged or split, so what you want is "$@". Unlike $*, $@ doesn't merge the arguments, and with double-quotes it doesn't split them either (or expand them as filename wildcards) -- it just passes them straight through. But don't use args="$@", because that actually does merge them like "$*" (or maybe with " " instead of the first char of IFS, depending on the shell) (it has to merge them to save them in a plain variable). Use args=("$@") instead, to save them as an array.

BTW2, in most situations you don't want the arguments either merged or split, so what you want is "$@". Unlike $*, $@ doesn't merge the arguments, and with double-quotes it doesn't split them either (or expand them as filename wildcards) -- it just passes them straight through. But don't use args="$@", because that actually does merge them like "$*" (it has to to save them in a plain variable). Use args=("$@") instead, to save them as an array.

BTW2, in most situations you don't want the arguments either merged or split, so what you want is "$@". Unlike $*, $@ doesn't merge the arguments, and with double-quotes it doesn't split them either (or expand them as filename wildcards) -- it just passes them straight through. But don't use args="$@", because that actually does merge them like "$*" (or maybe with " " instead of the first char of IFS, depending on the shell) (it has to merge them to save them in a plain variable). Use args=("$@") instead, to save them as an array.

Use "set --" when setting positional parameters rather than shell options.
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
$ echo "$*"

$ set -- "foo bar" "baz/quux"
$ echo "$*"
foo bar baz/quux
$ echo "$*"

$ set "foo bar" "baz/quux"
$ echo "$*"
foo bar baz/quux
$ echo "$*"

$ set -- "foo bar" "baz/quux"
$ echo "$*"
foo bar baz/quux
Added notes about "$@" and field splitting with read
Source Link
Gordon Davisson
  • 5.2k
  • 1
  • 21
  • 19
Loading
added 7 characters in body
Source Link
Gordon Davisson
  • 5.2k
  • 1
  • 21
  • 19
Loading
Source Link
Gordon Davisson
  • 5.2k
  • 1
  • 21
  • 19
Loading