User Tools

Site Tools


characters-and-expressions

Characters & Expressions


SPECIAL CHARACTERS (metacharacters) have a special meaning, the interpreter is a shell
$ 1) prompt, indicates readiness of the shell to accept commands; default sign for a normal user
2) value of the variable
$ car=jeep
$ echo $car
jeep
# 1) prompt, indicates readiness of the shell to accept commands; default sign for root user
2) comment in configuration files or scripts (each line starting with this character is ignored by the system)
\ backslash, indicates a text line wrapping
> "greater than", indicates a text line continuation
- hyphen, reads data from STDOUT
$ gzip -cd file.tar.gz | tar -xf -
-- double hyphen, indicates the end of options and disables further option processing, the next string is treated as an argument
$ mkdir -- -dir
(creates directory "-dir"; equivalent is mkdir ./-dir)
; semicolon, indicates the end of a command
$ cd /usr/sbin; ls -la
& ampersand, the shell executes the command in the background in a subshell (useful for running larger jobs)
&<n> file descriptor indicator
&& double ampersand, executes a command only if the previous command returns a zero exit status
$ mount /mnt/fd && cp -R /mnt/fd /home/user/xxx/ && umount /mnt/fd
|| double pipe, executes a command only if the previous command returns a non-zero exit status
$ cd ~/.ssh 2> /dev/null || mkdir ~/.ssh
(<list>) parentheses for a group of 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 for a group of 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 users || { mkdir users; cd users;}
REDIRECTION CHARACTERS redirect the standard input / output / error of a command
< / 0< standard input redirection (STDIN / file descriptor 0)
$ mail tom@atlas.cz < list.txt
> / 1> standard output redirection (STDOUT / file descriptor 1)
$ ls -la > dir_contents.txt
(the command output is redirected to the particular file which is created at the same time or overwritten in case it existed before)
$ cat file1 | tee file2 > file3
(the command output is redirected to more files at the same time)
$ wc -l < report > /tmp/lines
(the number of lines of "report" file is saved to "/tmp/lines")
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)
>> 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
<<EOF standard input redirection from a file (script)
#!/bin/bash
mail $user <<EOF
<text>
EOF
(redirects a text from the script up to the string "EOF" to STDIN of "mail" command)
$ cat <<END
> I am $LOGNAME. <-'
> END <-'
(interactive form)
| pipe, redirects the output of one command as an input for another command
$ cat notes | grep linux
(prints all lines of the file containing a "linux" string)
$ ls -l | grep "^d" | wc -l
(prints the number of subdirectories in the working directory)
tee <file> reads from standard input and writes to standard output and particular files, -a appends the output to the end of a file
$ ls -l | tee /tmp/test | wc -l
(the output of "ls" command is saved to "/tmp/test", the number of lines is displayed on STDOUT)
$ who | sort | tee -a log1 log2
(the sorted output of "who" command is written both to STDOUT and to files "log1" and "log2")
SUBSTITUTION CHARACTERS (wildcards) substitute characters for a detail file names specifications
* 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")
[...] square brackets, define a class 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]), "!" 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)
{...} braces, define a string (comma separated strings) to be used in the pattern; an optional preamble may precede the string that may be followed by a postcript which is appended to each string
$ 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)
`<list>` / $(<list>) backquotes, a command inside the backquotes is executed and replaced by its output
$ working_directory=`pwd`
$ echo $working_directory
/home/kuba
(the first command defines a variable "working_directory”, the latter prints its value)
$ ls -l `which java` / ls -l $(which java)
(the output of a command "which java” is passed to a command "ls -l”)
QUOTING CHARACTERS suppress the meaning of special characters
\ backslash, prevents the shell from interpreting the following single character
$ echo \$car
$car
'...' single quotes, prevent the shell from interpreting any string inside the quotes; single quotes cannot be quoted by themselves however
$ echo '$car'
$car
$ echo ''$car''
jeep
"..." double quotes, allow the interpretation of substitution characters, "$" and "`", "\" expands only when followed by "$", """, "`", "\" and a new line; double quotes cannot be quoted by themselves however
# find / -type d -name "[A-Z]*"
$ echo "`pwd`"
/home/kuba
$ echo "$car"
jeep
$ echo "\$car"
$car
$ echo ""$car""
jeep


REGULAR EXPRESSIONS (extended) specify the particular string in the text, the interpreter is a program
special characters: define the character of the expression
. dot, matches any single character including space
[...] square brackets, matches any single character in the list, "^” the caret at the beginning of the list excludes the following characters, "-" the hyphen specifies a range of characters
(...) parentheses, define a string
| pipe, means "or", separates particular strings
\ backslash, suppresses the meaning of the following special character
position characters: define the position of the expression
^ caret, matches the beginning of a line
$ dollar sign, matches the end of a line
\<...\> escaped angle brackets, matches a particular string
quantifiers: define the number of repetitions of the previous expression
? question mark, matches 0 or 1 times
* asterisk, matches 0 or more times
+ plus, matches 1 or more times
{n} numeric value in braces, matches exactly n times
{m,n} numeric value in braces, matches at least m times, but not more than n times
{m,} numeric value in braces, matches at least m times
{,n} numeric value in braces, matches at most n times
regular expression: equals to:
a. "a" + any single character
a+b "ab", "aab", "aaab" ...
a\+b "a+b"
s?care "scare" or "care"
auto(mobile)? "auto" or "automobile"
micro(phone|scope) "microphone" or "microscope"
^(Subject|From): a line starting with "Subject:" or "From:"
ha{1,3} "ha" or "haha" or "hahaha"
<T[DH]> "<TD>" or "<TH>"
\<a.*a\> a string starting and ending with "a"
\[[a-zA-Z]\] any letter enclosed in "[]"
[0-9]+ at least one number
[0-9]|[1-9][0-9] a range of numbers "0" - "99"
[0-9]{2} a range of numbers "00" - "99"
(19|20)[0-9]{2} a range of numbers "1900" - "2099"
[0-9a-fA-F]|[1-9a-fA-F][0-9a-fA-F]+ hexadecimal numbers
^$ an empty line
[.^az\-] a dot, caret, "a", "z", backslash or hyphen
[^ ,.]+ a string not containing a space, comma or dot
.+0$ a line ending with "0" and at least 1 preceding character
^P.*(0[1-9])$ a line starting with "P" and ending with "01" - "09"
[+0-9 /-]{9,} any phone number
[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} any e-mail address
(https?://)?(w{3}\.)?[a-zA-Z0-9.-]+\.[^ ]{2,} any web address
Last modified: 2015/07/23 15:25 by Miroslav Bernát

visits: