bash glob (Filename Expansion)

 

 


bash glob

 

 * Bash 不支援 regex, 而它支援的叫 globbing

Syntax

  • "?"          # Question mark match any single character
  • "*"          # Asterisk match zero or more characters
  • "[a-z]"     # Square Bracket match the character from the range
  • "^"         # Caret with Square Bracket("[]") for negating the sense of a match

Notes

  • "*" will not match filenames that start with a dot
  • echo * 相當於 ls
  • Filename expansion can match dotfiles,
    but only if the pattern explicitly includes the dot as a literal character
    ( *hide_file, ?hide_file, [.]hide_file 是 ls 不到的, 要用 ls .hide_file )

Example

touch a.sh b.sh c.sh d.sh a1.sh a2.sh .hide_file

ls *                  # a1.sh  a2.sh  a.sh  b.sh  c.sh  d.sh

ls a?.sh            # a1.sh  a2.sh

ls [b-d]*          # b.sh  c.sh  d.sh

ls [^a-c]*        # d.sh

ls .hide_file

 

 

 

Creative Commons license icon Creative Commons license icon