나는 큰 스크립트를 가지고 있지만이 작은 사람은 문제를 보여줍니다 변수가 while 루프 외부 설정이 변수가 변경되지 않는 이유는 무엇입니까?
#!/bin/bash
x=0
if [[ $x == 0 ]]
then
ls | while read L
do
x=5
echo "this is a file $L and this is now set to five --> $x"
done
fi
echo "this should NOT be 0 --> $x"
경우 내가 예상대로 다음 작동합니다. bash 버전은 3.2.25 (1) -release (x86_64-redhat-linux-gnu)입니다. 이것이 명백한 일이라면 나는 너무 바보 같은 기분이들 것이다.
이것은 일반적인 질문입니다. 자주 묻는 질문 (FAQ) 읽기 : http://mywiki.wooledge.org/BashFAQ/024 –
위대한 링크 : [ls의 출력을 구문 분석하지 않아야하는 이유] (http://mywiki.wooledge.org/ParsingLs) –
비슷한 것을 확인하십시오 thread : http://stackoverflow.com/questions/13233452/bash-variable-change-doesnt-persist. – konsolebox