<div dir="ltr">2014-06-01 18:46 GMT+02:00 Johnny Rosenberg <span dir="ltr"><<a href="mailto:gurus.knugum@gmail.com" target="_blank">gurus.knugum@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>I have a variable that contains a path. Now I want to copy every single directory to an array like this:</div><div>x=./abc/def/ghi/jkl/mno</div><div># Do something here to copy x to the array y…</div><div>

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

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

.<br>6<br>$</div><div><br></div><div>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?</div><span class=""><font color="#888888"><div><br></div><div><br></div>
<div>Johnny Rosenberg</div></font></span></div></div>
</blockquote><div><br></div><div>After some further ”googling” I finally found the answer:<br></div><div>$ x="./abc/def/ghi/jkl/mno"; IFS='/'; y=(${x}); echo ${y[0]}; echo ${#y[@]}; IFS=$'\n'<br>
.<br>6<br>$</div><div><br></div><div>Seems like there is a special array syntax, using those parentheses – ”y=(${x})”.</div><div>If anyone have anything else to say in this matter, please do. I want to learn.</div><div><br>
</div><div><br></div><div>Johnny Rosenberg</div></div>