![grep이 우분투에서 [0-9] 패턴을 제공할 때 예기치 않은 동작이 발생합니다.](https://linux55.com/image/186538/grep%EC%9D%B4%20%EC%9A%B0%EB%B6%84%ED%88%AC%EC%97%90%EC%84%9C%20%5B0-9%5D%20%ED%8C%A8%ED%84%B4%EC%9D%84%20%EC%A0%9C%EA%B3%B5%ED%95%A0%20%EB%95%8C%20%EC%98%88%EA%B8%B0%EC%B9%98%20%EC%95%8A%EC%9D%80%20%EB%8F%99%EC%9E%91%EC%9D%B4%20%EB%B0%9C%EC%83%9D%ED%95%A9%EB%8B%88%EB%8B%A4..png)
패턴을 사용하여 파일의 모든 숫자를 검색할 때 [0-9]
패턴 주위에 작은따옴표/큰따옴표를 사용하면 다른 결과가 생성됩니다.
$ cat numbers
this line has 3
this line has 4
this line has 2
this line has 8
this line has 1
$ grep [0-9] numbers
this line has 1
$ grep '[0-9]' numbers
this line has 3
this line has 4
this line has 2
this line has 8
this line has 1
[0-9]
이들그리고 의 차이점은 무엇인가요 '[0-9]'
? [a-z]
및 를 사용하여 유사한 테스트를 수행했지만 결과 '[a-z]'
는 이전 예제와 동일했습니다.
16.04.7 LTS(Xenial Xerus) 시스템에서 테스트했습니다. Mac에서 동일한 테스트를 실행하면 예상대로 작동합니다.
답변1
Arch Linux의 Bash 5.1.4나 Ubuntu 16.04에서는 재현할 수 없습니다.
$ docker run --interactive --rm --tty ubuntu:16.04 /bin/bash
# cat > numbers <<EOF
> this line has 3
> this line has 4
> this line has 2
> this line has 8
> this line has 1
> EOF
# grep [0-9] numbers
this line has 3
this line has 4
this line has 2
this line has 8
this line has 1
@Kusalananda가 올바른 생각을 가지고 있습니다, 이름이 "1"인 파일이 있을 수 있으며 따옴표가 없는 glob은 다음으로 확장됩니다 grep
.
# touch 1
# grep [0-9] numbers
this line has 1
답변:더 많은 인용문 사용™:)