이 bash 쉘 매개변수 확장을 이해할 수 없습니다.

이 bash 쉘 매개변수 확장을 이해할 수 없습니다.

다음 명령에 집착합니다.

declare -a partition_files
readarray -d '' partition_files < <(find "$choosen_image_folder" -name "*sda${i}.gz*")

# this does not work
/bin/cat "${partition_files[*]}" | /bin/gunzip -f -c | ntfsclone -r -O "/dev/sda$i" -
# this does work
/bin/cat ${partition_files[*]} | /bin/gunzip -f -c | ntfsclone -r -O "/dev/sda$i" -
# this does not work
/usr/sbin/partimage restore -b "/dev/sda$i" "${partition_files[*]}"
# this does work
/usr/sbin/partimage restore -b "/dev/sda$i" ${partition_files[*]}

이 경우 따옴표를 제거하면 작동하지만 사용하면 작동하지 않는 이유는 무엇입니까?

답변1

"${partition_files[*]}"모든 배열 요소를 연결하여하나쉘 단어의 경우 첫 번째 문자를 IFS커넥터로 사용하십시오. 따라서 배열이 a=("foo bar" asdf)이고 IFS기본값을 갖는 경우 "foo bar asdf"다음과 같은 결과를 얻습니다.

대신, "${partition_files[@]}"각 요소를 다른 단어로 만들어서 와 동일하게 만들고 싶습니다 "foo bar" "asdf".

"$@"이는 및 사이의 차이점과 같습니다 "$*". 일반적으로 특별한 작업을 수행하고 있다는 사실을 알지 않는 한 항상 "$@"또는 "${array[@]}"(at 기호와 따옴표 사용)이 필요합니다.

따옴표가 없으면 ${array[*]}모든 요소를 ​​개별적으로 가져오고 각 단어가 다시 분할됩니다. ( IFS빈 문자열이 아닌 경우 기본적으로 모든 요소를 ​​연결한 뒤 단어 분할을 합치는 것과 같습니다.)

예를 들어 참조하십시오.https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html

관련 정보