bash tutorial shell mode extension [MD]

Blog address

My GitHub My blog My Wechat My email
baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

catalogue

Schema Extension for Bash

This article is adapted from Bash tutorial of network channel , mainly to simplify a large number of contents that I am not interested in.

brief introduction

After receiving the command entered by the user, the Shell will split the user's input into tokens according to the spaces. Then, the Shell will extend the special characters (~,?, *, [], {}, $) in the word element. The corresponding command will not be called until the extension is completed. This extension of special characters is called globbing.

Bash extends first and then executes commands. Bash is responsible for the result of the extension, regardless of the command to be executed. There is no parameter extension in the command itself. Any parameter received will be executed as it is.

The English word of module extension is globbing, which comes from the early Unix system. There is a / etc/glob file to save the extended template. Bash later built this function, but the name remained.

The relationship between pattern extension and regular expression is that pattern extension appeared earlier than regular expression. It can be regarded as an original, early and low configuration regular expression. Its function is not as powerful and flexible as regular expression, but its advantage is simple and convenient.

set -f  # [set -o noglob] close the extension
set +f  # [set +o noglob] open the extension

File name extension

The file name extension character will be extended only if the file exists. If there is no matching file name, it will be output as is.

User home directory~

The wavy line ~ will expand to the home directory of the current user.

echo ~      # [/ home/bqt] the wavy line ~ will be expanded to the home directory of the current user
echo ~/foo  # [/ home/bqt/foo] a subdirectory of the main directory
echo ~root  # [/ root] the home directory of the user root
echo ~foo   # [~ foo] if the user name foo does not exist, it will be output as it is
echo ~+     # Expand to the current directory, which is equivalent to the [pwd] command

Single character?

Character? Represents any single character in the file path, excluding null characters.

ls ?.txt    # [a.txt] character? Represents any single character in the file path, excluding empty characters
ls ??.txt   # [ab.txt] if multiple characters are matched, multiple characters are required? Combined use
echo ?.bqt  # [?. bqt] if there is no matching file name, it will be output as is

Any character*

The character * represents any number of arbitrary characters in the file path, including zero characters* Will not match to Hidden file at the beginning

echo *.txt   # [a.txt ab.txt] the character * represents any number of arbitrary characters in the file path, including zero characters
echo *       # Output all files in the current directory, excluding files in the form of Hidden files at the beginning, excluding files in subdirectories
echo *.bqt   # [*. bqt] if there is no matching file name, it will be output as is

echo .*      # Output all hidden files in the current directory (i.e. files starting with.)
echo .[!.]*  # Hide the current directory and exclude all files (current directory) and (parent directory) these two hidden files

*Only the current directory will be matched, and no subdirectories will be matched

echo */      # Output all subdirectories of the current directory
echo */*     # Output all files in all subdirectories of the current directory
echo */*/    # Output all secondary subdirectories of the current directory
echo */*/*   # Output all files in all secondary subdirectories of the current directory. If there are several levels of subdirectories, you must write several levels of asterisks

After opening the globstar parameter, * * can match zero or more subdirectories of any depth (new in Bash 4.0)

shopt globstar    # Query whether a parameter is closed or open. The globstar parameter is closed by default
shopt -s globstar # After you open a parameter and the globstar parameter, * * can match zero or more subdirectories
shopt -u globstar # Close a parameter
echo **/*         # Output all files in the current directory and any depth subdirectory

Any character []

  • Square bracket extension [...] The characters in any bracket can be extended
  • Exclusion mode [^...] And [!...] You can extend any character that is not in square brackets
  • If you need to match the character [, it can be placed anywhere within square brackets. Matching characters are not supported]
  • If you need to match the character -, you can only put it at the beginning or end of the square brackets
echo [ab].txt  # Square bracket extension [...] The characters in any bracket can be extended
echo [^ab]     # Equivalent to [! ab], which means to extend any character not in square brackets
echo [[ab]     # If you need to match the character [, it can be placed anywhere within square brackets. Matching characters are not supported]
echo [-ab]     # If you need to match the character -, you can only put it at the beginning or end of the square brackets
echo [ab].bqt  # [[ab].bqt] if there is no matching file name, it will be output as is

The square bracket extension has a short form [start end], which means to match a continuous range.

  • [a-z]: all lowercase characters
  • [! a-z]: all non lowercase characters
  • [a-zA-Z]: all lowercase letters + uppercase letters
  • [a-zA-Z0-9]: all lowercase letters + uppercase letters + numbers
  • program.[co]: file program C and file program o
  • BACKUP.[0-9][0-9][0-9]: backup for all Start with a three digit file name

Character class [[: Class:]]

[[: Class:]] represents a character class, which is extended to one of a specific class of characters.

  • [[: digit:]: match any number 0-9
  • [[: lower:]: match any lowercase letter a-z
  • [[: upper:]: match any uppercase letters A-Z
  • [[: alpha:]: match any English letter
  • [[: alnum:]: match any English letters and numbers
  • [[: cntrl:]]: non printable characters of ASCII code 0-31
  • [[: print:]]: printable characters of ASCII code 32-127
  • [[: punch:]: punct uation (except printable characters a-z, a-z, 0-9)
  • [[: graph:]: match any English letters, numbers and punctuation marks
  • [[: blank:]: space and Tab
  • [[: Space:]: space, Tab, LF (10), VT (11), FF (12), CR (13)
  • [[: xdigit:]]: match hexadecimal characters (A-F, A-F, 0-9)
echo [[:upper:]]*  # Output all file names that begin with uppercase letters
echo [![:digit:]]* # Output all file names that do not start with a number
echo [[:blank:]]*  # [[[: blank:]] *] if there is no matching file name, it will be output as is

Quantifier grammar? + *@ ()

Quantifier grammar is used to control the number of pattern matching. It can only be used when the extglob parameter of Bash is turned on, but it is generally turned on by default.

shopt extglob     # Query whether a parameter is turned off or on. The extglob parameter is turned on by default
shopt -s extglob  # After you open a parameter and the extglob parameter, you can use quantifier syntax
shopt -u extglob  # Close a parameter
  • ? Pattern list: matches zero or one pattern
  • *(pattern list): matches zero or more patterns
  • +(pattern list): matches one or more patterns
  • @(pattern list): only one pattern is matched
  • ! (pattern list): matches anything other than a given pattern
ls abc?(def)       # [abc abcdef] matches files that start with abc and end with zero or one def
ls abc+(.txt|.php) # [abc.php abc.txt] match the match that starts with abc and ends with txt or php end file
ls abc+(.txt)      # [abc.txt abc.txt.txt] match with ABC and Txt file
ls a!(b)c.txt      # [aac.txt ac.txt] match except ABC Txt, which starts with a and ends with c.txt
echo abc*(.bqt)    # [abc*(.bqt)] if there is no matching file name, it will be output as is

String extension

All values {}

Brace extension {...} Indicates that all values in braces are expanded respectively.

  • The values are separated by commas, and the values can be multi character patterns
  • There can be no value before the comma, indicating that the first item of the extension is empty
  • There should be no spaces before and after the comma. Otherwise, Bash will think that this is not a brace extension, but an independent parameter
  • Braces can be nested, can be used in conjunction with other schemas, and always extend before other schemas
  • The curly bracket extension is not a file name extension, it always extends, which is the same as the square bracket extension [...] totally different
echo {1,2,3}          # [1 2 3] brace extension {...} Indicates that all values in braces are expanded respectively
ls {,a,abc}.txt       # Visit in turn txt a.txt abc.txt three files, regardless of whether the file exists or not
echo a{a{1,2},b{3,4}} # [aa1 aa2 ab3 ab4] nesting is supported
echo /bin/{cat,ba*}   # Braces can also be used in conjunction with other schemas and are always extended before other schemas

Brace extensions can be used directly with for loops.

for i in {1,2,4,Bai Qiantao}
do
  echo $i
done
  • The short form of brace extension {start..end} indicates that it is extended into a continuous sequence
  • Braces extend another shorthand form, start end.. Step to specify the extended step size
  • The short form supports reverse order and nesting
echo {01..5}          # [01 02 03 04 05] expand into a continuous sequence
echo {0..8..2}        # [0 2 4 6 8] the extended step size can be specified
echo {a..c}{1,2}      # [a1 a2 b1 b2 c1 c2] if multiple abbreviations are used together, it will have the effect of circular processing
echo .{mp{5..3},mkv}  # [. MP5. MP4. MP3. MKV] supports reverse order and nesting
echo {a1..3c}         # [{a1..3c}] in case of incomprehensible abbreviations, it will be output as is and will not be extended

Dollar sign extension

Variable extension$

Bash treats the words beginning with the dollar sign $as variables and expands them into variable values$ {! String *} or ${! String @} returns the names of all variables that match a given string.

echo $SHELL   # [/bin/bash]
echo ${SHELL} # [/ bin/bash] variable name can also be placed in ${}
echo ${!S*}   # Returns all variable names starting with S, or null if they do not exist

Subcommand extension $(...)

$(...) It can be extended to the running result of another command, and all outputs of the command will be used as return values.

echo $(date)       # Return the running result of the date command. Note that date is a command rather than a variable
echo `date`        # Placing a subcommand in backquotes can also be extended to the result of the command
echo $(ls $(pwd))  # Subcommand extensions can be nested

Arithmetic extension $(...)

$((...)) It can be extended to the result of integer operation.

echo $((2 + 2))  # 4

shopt command related to Schema Extension

The shopt command adjusts Bash's behavior. It has several parameters related to wildcard extension.

shopt [optionname]    # Query whether a parameter is closed or open
shopt -s [optionname] # Open a parameter
shopt -u [optionname] # Close a parameter
  • dotglob: closed by default. You can make the extension result include hidden files, that is, files starting with points.
  • nullglob: off by default. When the wildcard does not match any file name, it can return an empty string instead of the original.
  • failglob: closed by default. You can directly report an error when the wildcard does not match any file name, rather than continue to let each command handle it.
  • extglob: open by default, allowing Bash to support some extended syntax of ksh. Its main application is to support quantifier grammar.
  • nocaseglob: off by default. Wildcard extensions can be made case insensitive.
  • globstar: closed by default, allowing * * to match zero or more subdirectories (new in Bash 4.0).

Notes on the use of pattern extension

  • Wildcards are explained first and then executed: after Bash receives the command, if there are wildcards in it, it will first expand the wildcards and then execute the command.
  • The file name extension will be output as is when there is no matching file.
  • All file name extensions only match single-layer paths, and cannot match files in subdirectories (i.e., cannot match path separator /).
  • Bash allows file names to use wildcards, that is, file names include special characters. When referencing the file name, you need to put the file name in single quotation marks or double quotation marks.

2022-1-2

Added by itsmeArry on Mon, 03 Jan 2022 09:20:52 +0200