![분할 시 내 bash 배열의 길이가 항상 1인 이유는 무엇입니까?](https://linux55.com/image/118379/%EB%B6%84%ED%95%A0%20%EC%8B%9C%20%EB%82%B4%20bash%20%EB%B0%B0%EC%97%B4%EC%9D%98%20%EA%B8%B8%EC%9D%B4%EA%B0%80%20%ED%95%AD%EC%83%81%201%EC%9D%B8%20%EC%9D%B4%EC%9C%A0%EB%8A%94%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
# Create array
arrayLong=(one two three four)
for element in "${arrayLong[@]}"
do
echo "$element"
done
echo "${#arrayLong[@]}"
산출:
one
two
three
four
4
그 다음에:
# Make new array with only first half of values
arrayShort=("${arrayLong[@]:0:2}")
for element in "${arrayShort[@]}"
do
echo "$element"
done
echo "${#arrayShort[@]}"
이것의 출력은 다음과 같습니다
one two
1
내 짧은 배열이 실제로 배열이 아닌 이유는 무엇입니까? 이것은 단지 하나의 요소일 뿐입니다. 배열에 결과가 가득 차면 배열을 분할하는 방법은 무엇입니까?
내 배쉬 버전은GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
답변1
IFS
사용 후 자동으로 초기화되지 않는지 몰랐습니다 . 이전 코드에서는 IFS=$'\n'
원래 값을 저장하지 않고 설정했습니다. 이것이 내가 해야 할 일입니다:
# set Internal Field Separator to new line only to split files
oIFS="$IFS"
IFS=$'\n'
array=(${all_files})
# Return IFS to initial value
IFS="$oIFS"
IFS
특정 순간의 내용을 다시 확인하려면 printf "%q\n" "$IFS"
기본값은 입니다.$' \t\n'
도움을 주신 @MiniMax와 @Jesse_b에게 감사드립니다.