Скобки, фигурные скобки, фигурные скобки в Bash

Здесь идет загадка:

Если я делаю:

touch file{1,2,3}

Это создает file1, file2, file3

И если я делаю

rm file[1-3]

Это удаляет их.

но если я делаю

touch file[1-3] 

это создает:

file[1-3]

Почему?

9
задан 14 October 2015 в 14:37

1 ответ

Если Вы позаботились чтения страницы справочника вместо того, чтобы делать загадки:

Brace Expansion
   Brace  expansion  is  a  mechanism  by  which  arbitrary strings may be
   generated.  This mechanism is similar to pathname  expansion,  but  the
   filenames generated need not exist. 
...
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
   Pattern  Matching  below). If no matching filenames are found, and the
   shell option nullglob is not enabled, the word is left  unchanged.
...
Pattern Matching

   Any character that appears in a pattern, other than the special pattern
   characters described below, matches itself.  ...

   The special pattern characters have the following meanings:

   ...
          [...]  Matches  any  one  of the enclosed characters.  A pair of
                 characters  separated  by  a  hyphen  denotes   a   range
                 expression;  any  character  that falls between those two
                 characters,  inclusive,  using   the   current   locale's
                 collating sequence and character set, is matched.

file[1-3] расширяется в названные файлы file1, file2, file3. Расширение имени файла происходит, только если существуют соответствующие файлы. В противном случае шаблон оставляют как есть. Поэтому с названными файлами file1, file2, file3, file[1-3] расширяется до file1 file2 file3. Без этих файлов это не расширяется и остается как file[1-3]. С {...}, имена файлов не должны существовать, таким образом, file{1..3} расширяется до file1 file2 file3 независимо от файлов, присутствующих или бывших отсутствующий.

13
ответ дан 23 November 2019 в 04:57

Другие вопросы по тегам:

Похожие вопросы: