0
AVAILABLEDIR=("${AVAILABLEDIR[@]}" "$(ls $LOC -AFl | sed "1 d" |  grep "/$" | awk '{ print $9,$10 }')")

I'm trying to create an array using this command, however when it adds objects to the array, it adds $9 and $10 seperately, is there a way to tell the array to have both of these arguments joined? This is what I want:

[Directory 1/] [Directory 2/] [Directory 3/]

instead of

[Directory] [1/] [Directory] [2/] [Directory] [3/]

Thank you for your help

1
  • It looks like you ask about that homework twice a day, and at least one of my answers shows how to scan a directory even if the entry names contains spaces. Commented Jan 1, 2015 at 19:29

1 Answer 1

3

Don't use a pipeline headed by ls for this; just use a glob.

pushd "$LOC"
AVAILABLEDIR+=( */ )
popd

pushd works like cd, but saves the current directory on a stack before changing. */ is a pattern that matches all directory names in the current directory; += appends the matching directories to the current value of AVAILABLEDIR. popd removes the directory name from the top of the stack and cds there. The pushd/popd combination is the easiest way to add Directory 1, rather than $LOC/Directory 1, to the array.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't really understand this code, is there a simpler way?
I literally do not know how this could be any simpler (using length as the measurement of complexity) but I will add an explanation for what each part does, which is worth understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.