User Tools

Site Tools


characters-and-expressions

Characters & Expressions


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)
# 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)
\ 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
- dash
1) specifies the command options
$ ls -a
2) reads data from STDIN/STDOUT
$ gzip -cd file.tar.gz | tar -xf -
-- double dash
1) specifies the command (long) options
$ ls --all
2) indicates the end of options and disables further option processing (the next string is treated as an argument)
$ mkdir -- -dir
(creates directory "-dir"; the equivalent is "mkdir ./-dir")
; semicolon, indicates the end of the command (the same as the new line)
$ cd /tmp; ls -la
& ampersand, the shell executes the command in the background in a subshell (useful for running larger jobs; the return code is always zero)
&& 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, 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;}
{<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)
$((<expression>)) / $[<expression>] dollar and double parenthes/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”)
PARAMETERS entities in which values ​​are stored
Variables: "storage" of information, consist of the name and assigned value
<variable>=<value> defines a local variable (it remains defined in the shell until its termination)
$ xy=day
$ working_directory=$(pwd) / working_directory=`pwd`
(the value of the variable can also be the output of the command)
$ PATH="$PATH:~/scripts"
(adds "scripts” directory to the "PATH” variable)
$ PS1="[\u@\h \W]\$ "
(sets the user's prompt; the "PS1” variable can be defined this way: \u = user, \h = hostname, \W = working directory)
# for home in $(awk -F : '{print $6}' /etc/passwd); do ls -l ${home}/.ssh/id_[rd]sa 2> /dev/null | egrep -v "^-rw-------"; done
(prints files containing private keys of all users whose permissions differ from value of 600)
$<variable> / ${<variable>} the value of a variable
$ echo $xy
day
$ echo it is a new week$xy / echo "it is a new week$xy"
it is a new weekday
$ echo "${xy}long period"
daylong period
(a space is needed after the variable otherwise it has to be put in "{}")
$ echo $working_directory
/home/root
$ echo $SHELL
(prints a user's login shell)
$ echo $TERM
(prints the type of the terminal; the list of terminals is available in /usr/share/terminfo)
${#<variable>} the length of a variable (number of characters)
Positioning parameters: digits representing the contents of arguments entered on the command line in a given order
$<n> / ${<n>} $ ./backup.sh /media/disk-1
(determines the device where the backup is made; the script includes e.g. "target_dir="$1"”)
Special parameters: parameters whose content can only be read
$<parameter> / ${<parameter>} the value of a parameter
$ echo $0
(prints the name of the current shell or shell script)
$ echo $$
(prints the PID of the current shell)
$ echo $?
(prints the RC of the previous foreground process)
$ echo $!
(prints the PID of the last background process)
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 | 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) 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)
? question mark, substitutes any single character, except a leading dot
# find / -name "*.19??"
(finds all files with a suffix ".19xx")
[...] 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
\ 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 the 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
Positioning 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: 2019/01/15 08:01 by Miroslav Bernát

visits: