Why doesn't printf "${array[@]}\n" print all elements of my array? Why doesn't printf "${array[@]}\n" print all elements of my array? unix unix

Why doesn't printf "${array[@]}\n" print all elements of my array?


The first argument to printf is the format string. Data should be passed only in subsequent arguments. Thus:

printf '%s\n' "${pwd_ids[@]}"

will properly emit:

E.1.1.7E.1.1.9E.1.1.2E.1.1.3E.1.1.4E.1.1.6E.1.1.5

Other format strings can be used as well; to print your items with a dash before them, for instance, you could use: printf ' - %s\n' "${pwd_ids[@]}"; or to print two to a line in columns padded out to 20 spaces, printf '%20s%20s\n' "${pwd_ids[@]}"


Or, to put the values all on one line, pass them all in a single subsequent argument:

printf '%s\n' "${pwd_ids[*]}"

With the output (if your IFS variable is at its default or otherwise starts with a space):

E.1.1.7 E.1.1.9 E.1.1.2 E.1.1.3 E.1.1.4 E.1.1.6 E.1.1.5

To explain all the above: Subsequent arguments are substituted for placeholders in the format string. In the first case above, each element of your array is evaluated against %s\n, and thus has a newline added immediately after it.

In your question, you're passing E.1.1.7 as a format string. This format string has no placeholders at all, so what the shell does with it is undefined: In your version of ksh, it prints the format string alone and ignores subsequent arguments; in other shells, it may print the format string (E1.1.7) once per argument, ignoring those arguments' values.