Copy variable to array

Johnny Rosenberg gurus.knugum at gmail.com
Sun Jun 1 16:46:47 UTC 2014


I have a variable that contains a path. Now I want to copy every single
directory to an array like this:
x=./abc/def/ghi/jkl/mno
# Do something here to copy x to the array y…
echo ${y[3]} # Should print ”def”.

Here's the result of my best (?) try:
$ x="./abc/def/ghi/jkl/mno"; IFS='/'; y=$x; echo ${y[0]}; echo ${#y[@]};
IFS=$'\n'
. abc def ghi jkl mno
1
$

Here's what I hoped would happen:
$ x="./abc/def/ghi/jkl/mno"; IFS='/'; y=$x; echo ${y[0]}; echo ${#y[@]};
IFS=$'\n'
.
6
$

What am I doing wrong? It seems to work if I read the path from a file with
”read -r -a”, why not from a variable?


Johnny Rosenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20140601/e8dd2b43/attachment.html>


More information about the ubuntu-users mailing list