I tried to display the content of the variable fpath in column form using parameter expansion, and I couldn't do it.
After some trying and web browsing I just found another solution; which does exactly what I want. It is:
print -C1 $fpath
But I still wonder how to do it the other way. The closest I got was:
echo ${fpath//' '/'\n'}
But I fail to identify the white space character, so the '\n' substitution never takes place. I tried to no avail:
echo ${fpath// /'\n'}
echo ${fpath//'\t'/'\n'}
I wonder, out of curiosity, what I am doing wrong.
Best Answer
fpath
is an array. You should not try to demote it to a string and then replace characters in it with newlines.With
ksh
-like array expansion syntax:With
zsh
, you can also use"$fpath[@]"
or"${(@)fpath}"
.You can also do:
But note that it skips empty elements of the array (likely not a problem for
$fpath
).zsh
'sprint
builtin can also print elements one per line with the-l
option. Like inksh
where theprint
builtin comes from, you do need the-r
option though to print arbitrary data raw, so:would be equivalent to the standard
printf '%s\n'
.They differ from
-C1
when$fpath
is an empty list in which caseprint -rC1 -- $fpath
outputs nothing whileprint -rl
andprintf '%s\n'
output one empty line, sois probably the closest to what you want to print elements of the array one per line which in
bash
/ksh
you could write: