function join1 {
typeset IFS=, # typeset makes a local variable in ksh²
print -r -- "$*" # using print instead of unreliable echo³
}
join1 a b c # => a,b,c
function join2 {
typeset IFS=$1IFS="$1"
shift
print -r -- "$*"
}
join2 + a b c # => a+b+c
This is almost always what you want. It expands each positional parameter to a separate word, which makes it perfect for taking command line or function arguments in and then passing them on to another command or function. And because it expands using double quotes, it means things don't break if, say, "$1"
contains a space or an asterisk (*
)³4.
³ echo
will or may fail to print its arguments properly if the first starts with -
or any contain backslashes, print
can be told not to do backslash processing with -r
and to guard against argument starting with -
or +
with the --
(or -
) option delimiter. printf '%s\n' "$*"
would be the standard alternative but note that ksh88 and pdksh and some of its derivatives still don't have printf
builtin.
4 Note that "$@"
didn't work properly in the Bourne shell and ksh88 when $IFS
didn't contain the space character, as effectively it was implemented as the positional parameter being joined with "unquoted" spaces and the result subject to $IFS
splitting. Early versions of the Bourne shell also had that bug that "$@"
was expanded to one empty argument when there was no positional parameter, which is one of the reasons why you sometimes see ${1+"$@"}
in place of "$@"
. BothNeither of those bugs don't affect modern Bourne-like shells.
45 The Almquist shell and bosh
have local
for that instead. bash
, yash
and zsh
also have typeset
, aliased to local
(also to declare
in bash and zsh) with the caveat that in bash
, local
can only be used in a function.