0

Please feel free to rename the question to something more appropriate.

How would I mimic the below zsh using bash instead?

mkdir folder1
mkdir folder2
mkdir folder3

# zsh

folders=(folder*) | print $folders
#folder1 folder2 folder3

# bash

folders=(folder*/) | echo $folders
#folder1

As you can see, this only outputs the first element.

Any pointers would be appreciated, thanks.

2
  • Are your folders in the same directory ? Commented Feb 28, 2014 at 16:58
  • Thanks for commenting. Yes they are. Commented Feb 28, 2014 at 17:00

2 Answers 2

4

Try changing it to:

folders=(folder*); echo "${folders[@]}"
  • folders[@] gives all the elements in the array
  • ${} expands the output from above command to bash.
Sign up to request clarification or add additional context in comments.

2 Comments

@jO It works if you separate the echo to a different line, or if you add a semicolon between the commands instead of a pipe.
This works as-is for me. Just copy and paste. @jO, your use of the pipe operator is incorrect in bash, it is probably part of your problem. This answer correctly uses semicolon.
0

If lets say, you have multiple .txt file in some Directory and you want to get/display those folders . you can try something like this:

declare -a folder_arr
i=0


for dir in *.txt; do

folder_arr[i]=$dir
i=$((i+1))
done

for j in $(seq 0 $((i-1)))
do
echo ${folder_arr[$j]}
done

I excuted the above file and was able to get the expected reult.

   /temps$ ./dirrr.sh
    z.txt

2 Comments

Do not iterate over the output of find like this. See this bash pitfall for details.
Hey @chepner Thanks for the feedback. You are right....I didnt have access to a linux machine earlier to try it...but, know I do. I have fixed it as per the "this bash pitfall" and now it work fine..... TAKE A BOW :)

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.