Copy variable to array

Tony Arnold tony.arnold at manchester.ac.uk
Sun Jun 1 17:02:44 UTC 2014


On 01/06/14 17:47, Johnny Rosenberg wrote:
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
$
Try this instead:

x="./abc/def/ghi/jkl/mno"; IFS='/'; y=($x); echo ${y[*]}; echo ${#y[@]}; IFS=$'\n'

For me, this produces:

. abc def ghi jkl mno
6

Regards,
Tony.

--
Tony Arnold,                        Tel: +44 (0) 161 275 6093
Head of IT Security,                Fax: +44 (0) 705 344 3082
University of Manchester,           Mob: +44 (0) 773 330 0039
Manchester M13 9PL.                 Email: tony.arnold at manchester.ac.uk<mailto:tony.arnold at manchester.ac.uk>

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


More information about the ubuntu-users mailing list