Frage

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.

War es hilfreich?

Lösung

Try changing it to:

folders=(folder*); echo "${folders[@]}"
  • folders[@] gives all the elements in the array
  • ${} expands the output from above command to bash.

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top