5

Using the default value of IFS, consider

unset var
set -- ${var:-"hello"}
echo "$#"

This prints 1 as the parameter expansion expands to the single string hello.

unset var
set -- "${var:-"hello"}"
echo "$#"

This too outputs 1 for the same reason.

However, if using ksh93, the following outputs 3 while dash, bash, pdksh, and zsh outputs 1.

unset var
set -- "${var:-" hello "}"
echo "$#"

In the ksh93 case, one gets 3 and "$@" is two empty strings as $1 and $3 while $2 is the string hello with no flanking spaces. The expanded command seems to be set -- "" hello "" in ksh93 and set -- " hello " in other shells.

A related (I believe) issue is

unset var
set -- ${var:-""}
echo "$#"

which outputs 1 in all sh-like shells, except for in ksh93 where it outputs 0. In ksh93, the quotes seems to be removed from the empty string before field splitting is performed.

Could the ksh93 behaviour be explained and supported by how POSIX says that the expansions etc. should be performed?

For the last of the two examples where behaviour between the shells differ, I personally think it's a bug (the command should expand from set -- ${var:-""} to set -- "", not to set --), but for the first such example (the one with set -- "${var:-" hello "}") I'm not so sure.


For testing ksh93 I'm using "Version AJM 93u+ 2012-08-01" on OpenBSD 6.4, and also "Version A 2017.0.0-devel-2040-g36a76aca", which is the current development version of the shell.

Your Answer

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

Browse other questions tagged or ask your own question.