V
V
Vadim2020-11-18 19:36:56
linux
Vadim, 2020-11-18 19:36:56

Where do new line characters disappear from the array of elements?

Hello everyone,

I don’t understand where new line characters disappear in the following code:

#!/bin/bash

declare -a dir_array

while IFS=  read -r -d $'\0'; do
        tmp="${REPLY#./}"
        dir_array+="$tmp\n"
done < <(find . -maxdepth 1 ! -path . -type d -print0)

readarray -t sorted < <(for a in "${dir_array[@]}"; do printf "$a"; done | sed '/^*$/d' | sort)

emp="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"

printf "${dir_array[*]}"
printf $emp
printf $emp
printf $emp
printf "${sorted[*]}"


In this case, the first array, dir_array, is printed correctly, in a column, and the second is sorted with a solid line?

all the best,
Vadim

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentin Sudakov, 2020-12-16
@Viji

Add, IFS=$'\n'; above readarray -t and everything will work =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question