User Tools

Site Tools


characters-and-expressions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
characters-and-expressions [2019/04/12 20:37]
Miroslav Bernát
characters-and-expressions [2019/08/06 13:11] (current)
Miroslav Bernát
Line 3: Line 3:
 ^ SPECIAL CHARACTERS (metacharacters) ^ have a special meaning, the interpreter is the shell ^ ^ SPECIAL CHARACTERS (metacharacters) ^ have a special meaning, the interpreter is the shell ^
 | **$** | dollar\\ prompt, indicates readiness of the shell to accept commands (default notation for a common user) | | **$** | dollar\\ prompt, indicates readiness of the shell to accept commands (default notation for a common user) |
-| **#** | hash\\ 1) prompt, indicates readiness of the shell to accept commands (default notation for root user)\\ 2) comment in configuration files or scripts (each word introduced after this character is ignored by the system, the exception is "#​!"​ (sha-bang) at the beginning of the script followed by the absolute path to the shell or the program to execute the file) |+| **#** | hash\\ 1) prompt, indicates readiness of the shell to accept commands (default notation for root user)\\ 2) comment in configuration files or scripts (each word introduced after this character is ignored by the system, the exception is "#​!"​ (shebang) at the beginning of the script followed by the absolute path to the shell or the program to execute the file) |
 | **\** | backslash (at the end of the line), indicates the line break of the command | | **\** | backslash (at the end of the line), indicates the line break of the command |
 | **>** | sign „greater than“ – $PS2 (at the beginning of the line), indicates the continuation of the command line | | **>** | sign „greater than“ – $PS2 (at the beginning of the line), indicates the continuation of the command line |
Line 14: Line 14:
 | **(**<​list>​**)** | parentheses,​ group commands (commands are executed in a subshell)\\ ''​$ (a; b) & c &''​\\ (commands „a“ and „b“ are executed sequentially in the background, i.e. one by one, command „c“ is executed simultaneously)\\ ''​$ (sleep 30m; echo "end of shift"​) &''​ | | **(**<​list>​**)** | parentheses,​ group commands (commands are executed in a subshell)\\ ''​$ (a; b) & c &''​\\ (commands „a“ and „b“ are executed sequentially in the background, i.e. one by one, command „c“ is executed simultaneously)\\ ''​$ (sleep 30m; echo "end of shift"​) &''​ |
 | **{** <​list>​**;​}** | braces, group commands (commands are executed in the current shell)\\ ''​$ { a; b;} & c &''​\\ (commands „a“ a „b“ are executed sequentially in the background, i.e. one by one, command „c“ is executed simultaneously)\\ ''​$ cd backup || { mkdir backup; cd backup;​}''​ | | **{** <​list>​**;​}** | braces, group commands (commands are executed in the current shell)\\ ''​$ { a; b;} & c &''​\\ (commands „a“ a „b“ are executed sequentially in the background, i.e. one by one, command „c“ is executed simultaneously)\\ ''​$ cd backup || { mkdir backup; cd backup;​}''​ |
-| **{**<​string>​**}** | braces, define a string (comma separated strings) to be used in the pattern; a prefix or suffix can be added to each string that becomes part of the expression\\ ''​$ mkdir p{la,​ri,​oi}nt''​\\ (creates directories „plant”,​ „print” and „point”)\\ ''​$ mv text{1,​2,​3,​4,​5}.txt text_0{1,​2,​3,​4,​5}.txt''​\\ ''​$ touch .{a,​b,​c,​d,​e}''​\\ (creates hidden files of the particular names)\\ ''​$ touch {1..100}''​\\ (creates 100 files)\\ ''​$ echo {A..Z} {a..z} {0..9}''​\\ (displays the range of given characters)\\ ''​$ echo {A..Z}{a..z}{0..9}''​\\ (displays three-figure combinations of characters in the particular order) |+| **{**<​string>​**}** | braces, define a string (comma separated strings) to be used in the pattern; a prefix or suffix can be added to each string that becomes part of the expression\\ ''​$ mkdir p{la,​ri,​oi}nt''​\\ (creates directories „plant”,​ „print” and „point”)\\ ''​$ mv text{1,​2,​3,​4,​5}.txt text_0{1,​2,​3,​4,​5}.txt''​\\ ''​$ touch .{a,​b,​c,​d,​e}''​\\ (creates hidden files of the particular names)\\ ''​$ touch {1..100}''​\\ (creates 100 files)\\ ''​$ echo {A..Z} {a..z} {0..9}''​\\ (displays the range of specified ​characters)\\ ''​$ echo {A..Z}{a..z}{0..9}''​\\ (displays three-figure combinations of characters in the particular order) |
 | **%%$((%%**<​expression>​**%%))%%** / **$[**<​expression>​**]** | dollar and double parentheses / dollar and square brackets, the arithmetic expression inside is evaluated and replaced by its result\\ ''​$ echo "​2*5=$%%((2*5))%%"''​ / ''​echo "​2*5=$[2*5]"''​\\ ''​2*5=10''​\\ ''#​ uid=500; for usr in a b c; do useradd -u $uid -g appadmin -d /home/$usr -m $usr; chage -M -1 $usr; uid=$%%(($uid+1))%%;​ done''​\\ (creates three application accounts whose UID starts with „500” and is growing by „1”) | | **%%$((%%**<​expression>​**%%))%%** / **$[**<​expression>​**]** | dollar and double parentheses / dollar and square brackets, the arithmetic expression inside is evaluated and replaced by its result\\ ''​$ echo "​2*5=$%%((2*5))%%"''​ / ''​echo "​2*5=$[2*5]"''​\\ ''​2*5=10''​\\ ''#​ uid=500; for usr in a b c; do useradd -u $uid -g appadmin -d /home/$usr -m $usr; chage -M -1 $usr; uid=$%%(($uid+1))%%;​ done''​\\ (creates three application accounts whose UID starts with „500” and is growing by „1”) |
 | **`**<​command>​**`** / **$(**<​command>​**)** | backquotes (backticks)/​dollar and parentheses,​ the command inside the backquotes is processed and replaced with its output\\ ''​$ ls -l `which java`''​ / ''​ls -l $(which java)''​\\ (the output of the command „which java” is passed to the command „ls -l”) | | **`**<​command>​**`** / **$(**<​command>​**)** | backquotes (backticks)/​dollar and parentheses,​ the command inside the backquotes is processed and replaced with its output\\ ''​$ ls -l `which java`''​ / ''​ls -l $(which java)''​\\ (the output of the command „which java” is passed to the command „ls -l”) |
Line 31: Line 31:
 | **2>** | standard error redirection (STDERR / file descriptor 2)\\ ''​$ cat file1 file2 > file3 2> /​dev/​null''​\\ (the error output of the command is not displayed)\\ ''​$ make all 2> /​dev/​pts/​3''​\\ (the error output of the command is displayed in another terminal) | | **2>** | standard error redirection (STDERR / file descriptor 2)\\ ''​$ cat file1 file2 > file3 2> /​dev/​null''​\\ (the error output of the command is not displayed)\\ ''​$ make all 2> /​dev/​pts/​3''​\\ (the error output of the command is displayed in another terminal) |
 | **2>&​1** | standard error redirection to standard output (the order of redirections is significant)\\ ''​$ ls sb1 sb2 > sb3 2>&​1''​ / ''​ls sb1 sb2 &> sb3''​\\ (STDOUT and STDERR is redirected only to sb3)\\ ''​$ ls file1 file2 2>&1 > file3''​\\ (STDERR is displayed on the screen and STDOUT is saved to file3)\\ ''​$ make all 2>&1 | less''​\\ (recommended especially when a process makes more error messages) | | **2>&​1** | standard error redirection to standard output (the order of redirections is significant)\\ ''​$ ls sb1 sb2 > sb3 2>&​1''​ / ''​ls sb1 sb2 &> sb3''​\\ (STDOUT and STDERR is redirected only to sb3)\\ ''​$ ls file1 file2 2>&1 > file3''​\\ (STDERR is displayed on the screen and STDOUT is saved to file3)\\ ''​$ make all 2>&1 | less''​\\ (recommended especially when a process makes more error messages) |
 +| **&>​** | standard output and error redirection\\ ''#​ yum install -y libstdc++.x86_64 &> /​dev/​null''​ |
 | **>>​** | standard output redirection to the end of a file\\ ''​$ tail /​var/​log/​messages >> logs.txt''​\\ (the command output is appended to the end of a particular file, if it does not exist, it is created) | | **>>​** | standard output redirection to the end of a file\\ ''​$ tail /​var/​log/​messages >> logs.txt''​\\ (the command output is appended to the end of a particular file, if it does not exist, it is created) |
 | **2>>​** | standard error redirection to the end of a file\\ ''#​ fsck /dev/sda1 2>> error''​ | | **2>>​** | standard error redirection to the end of a file\\ ''#​ fsck /dev/sda1 2>> error''​ |
Line 37: Line 38:
 | **SUBSTITUTION CHARACTERS (wildcards)** | **used to specify file names** | | **SUBSTITUTION CHARACTERS (wildcards)** | **used to specify file names** |
 | ***** | asterisk, substitutes zero or more characters, except a leading dot\\ ''#​ rm -Rf /​*''​\\ (removes the whole system)\\ ''​$ ls *' '​*''​\\ (prints files from the working directory containing space in their names) | | ***** | asterisk, substitutes zero or more characters, except a leading dot\\ ''#​ rm -Rf /​*''​\\ (removes the whole system)\\ ''​$ ls *' '​*''​\\ (prints files from the working directory containing space in their names) |
-| **?** | question mark, substitutes any single character, except a leading dot \\ ''#​ find / -name "​*.19??"''​\\ (finds all files with a suffix „.19xx“) |+| **?** | question mark, substitutes any single character, except a leading dot \\ ''#​ find / -name "​*.19??"''​\\ (finds all files with a suffix „.19xx“)\\ ''#​ find / -name "???​*"''​\\ (finds all files whose names contain at least 3 characters) |
 | **[**...**]** | square brackets, define a set of characters which of them only one is used in the pattern; „-“ means a range of characters (e.g. [A-Z], [a-z] or [0-9]), „!“ or „%%^%%“ at the beginning of the sequence stands for an exclusion\\ ''​$ ls -l text_[ab].txt''​\\ (lists files „text_a.txt” and „text_b.txt”)\\ ''​$ ls -l text_*[ab]*.txt''​\\ (lists files containing a combination of the particular characters in their names)\\ ''​$ rm -f .[!.]*''​\\ (removes all hidden files in the current directory) | | **[**...**]** | square brackets, define a set of characters which of them only one is used in the pattern; „-“ means a range of characters (e.g. [A-Z], [a-z] or [0-9]), „!“ or „%%^%%“ at the beginning of the sequence stands for an exclusion\\ ''​$ ls -l text_[ab].txt''​\\ (lists files „text_a.txt” and „text_b.txt”)\\ ''​$ ls -l text_*[ab]*.txt''​\\ (lists files containing a combination of the particular characters in their names)\\ ''​$ rm -f .[!.]*''​\\ (removes all hidden files in the current directory) |
 | **QUOTING CHARACTERS** | **suppress the meaning of special characters** | | **QUOTING CHARACTERS** | **suppress the meaning of special characters** |
Line 51: Line 52:
 | **|** | pipe, means „or“, separates particular strings | | **|** | pipe, means „or“, separates particular strings |
 | **\** | backslash, suppresses the meaning of the following special character | | **\** | backslash, suppresses the meaning of the following special character |
 +| **%%[[:​alpha:​]]%%** / **[a-zA-Z]** | alphabetic characters |
 +| **%%[[:​lower:​]]%%** | lower-case letters |
 +| **%%[[:​upper:​]]%%** | upper-case letters |
 +| **%%[[:​digit:​]]%%** / **[0-9]** | numeric characters |
 +| **%%[[:​alnum:​]]%%** / **[0-9a-zA-Z]** | alphanumeric characters |
 +| **%%[[:​punct:​]]%%** | punctuation characters |
 +| **%%[[:​print:​]]%%** | printable characters |
 +| **%%[[:​blank:​]]%%** | space or tabulator |
 | **Positioning characters:​** | **define the position of the expression** | | **Positioning characters:​** | **define the position of the expression** |
 | **^** | caret, matches the beginning of a line | | **^** | caret, matches the beginning of a line |
 | **$** | dollar sign, matches the end of a line | | **$** | dollar sign, matches the end of a line |
-| **\<​**...**\>​** | escaped angle brackets, matches a particular ​string |+| **\<​**...**\>​** | escaped angle brackets, matches a string ​that is not surrounded by a letter, number or underscore ​|
 | **Quantifiers:​** | **define the number of repetitions of the previous expression** | | **Quantifiers:​** | **define the number of repetitions of the previous expression** |
 | **?** | question mark, matches 0 or 1 times | | **?** | question mark, matches 0 or 1 times |
Last modified: 2019/04/12 20:37 by Miroslav Bernát

visits: