!["ls[0-9]"가 "[0-9]"라는 이름의 파일을 찾고 더 나쁜 것은 "touch 1" 이후가 아닌 이유는 무엇입니까?](https://linux55.com/image/180830/%22ls%5B0-9%5D%22%EA%B0%80%20%22%5B0-9%5D%22%EB%9D%BC%EB%8A%94%20%EC%9D%B4%EB%A6%84%EC%9D%98%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EC%B0%BE%EA%B3%A0%20%EB%8D%94%20%EB%82%98%EC%81%9C%20%EA%B2%83%EC%9D%80%20%22touch%201%22%20%EC%9D%B4%ED%9B%84%EA%B0%80%20%EC%95%84%EB%8B%8C%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)
$ mkdir temp && cd temp
$ ls [0-9]
ls: cannot access '[0-9]': No such file or directory
$ touch \[0-9\]
$ ls [0-9]
'[0-9]'
$ touch 1
$ ls
1 '[0-9]'
$ ls [0-9]
1
나는 이 행동이 매우 놀랍다고 생각한다. 나에게 [0-9]
전역 패턴은 이름이 단일 숫자로 구성된 파일과만 일치해야 합니다. 하지만 이것도때때로[0-9]
자체 이름의 파일 과 일치합니다 .
이것은 bash의 전역 확장 버그입니까?
[0-9]
일치하지 않도록 [0-9]
(하면 안 되기 때문에) 이것을 비활성화할 수 있습니까 ?
답변1
failglob
다음 명령을 사용하여 이 옵션을 설정 해야 합니다 shopt -s failglob
.
$ ls [2-9]
ls: cannot access '[2-9]': No such file or directory
$ touch '[2-9]'
$ ls [2-9]
[2-9]
$ shopt -s failglob
$ ls [2-9]
bash: no match: [2-9]
추가 질문: 끝에서 두 번째 ls가 선행 공백을 인쇄하는 이유는 무엇입니까?
새로운 '인간화' 때문에기본 인용 스타일GNU ls:
$ touch 1
$ unset QUOTING_STYLE # use the default
$ ls
1 '[2-9]'
$ QUOTING_STYLE=literal ls
1 [2-9]
$ ls --quoting-style=literal
1 [2-9]
답변2
에서 man bash
:
Pathname Expansion After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of filenames matching the pattern (see Pat‐ tern Matching below). If no matching filenames are found, and the shell option nullglob is not enabled, the word is left unchanged.
주의를 기울이다일치하는 파일 이름이 없고 쉘 옵션 nullglob이 활성화되지 않은 경우 단어는 변경되지 않고 그대로 유지됩니다.. 이것이 초기 테스트 결과가 다음과 같은 이유입니다.
ls: cannot access '[0-9]': No such file or directory