• let expression, expr expression, $((expression)), $((expression1, ex- • head/tail – print begin/end, tailf, tail -f wait for new
ailf, tail -f wait for new lines
pression2)), $[expression] • split, csplit – split file by size, content
• $LINENO – current line number in executed script • Numeric systems: base#number; hexa 0xABC, octal 0253, binary 2#10101011 • sort – -n numerical, -r reverse, -f ignore case
Bourne-Again
Bourne-Again SHell
SHell and
and Linux
Linux CLI
CLI
• $PATH – list of paths to executable commands
• $IFS – Internal field separator. List of chars, that delimiter words from
• Operators: i++, ++i, i--, --i, +, -; ** power, *, /, % remainder; logical: ! neg., &&
and, || or; binary: ~, &, |; <<, >> shifts; assignment: = *= /= %= += -= <>= &= ^= |=
•
•
uniq – omit repeated lines, -d show only duplicates
sed -e 'script' – stream editor, script y/ABC/abc/ replaces A, B, C for
© 2021 Martin ’BruXy’ Bruchanov,
[email protected] input, usually space, tabulator $'\t' and new line $'\n'. >>= <<=; relations: < <= > >= a, b, c; s/regexp/substitution/
Set interpreter: #!/bin/bash Remarks: # this is comment 4. Script command line parameters • factor n – factorize 𝑛 into primes • tr a b – replace char a for b
• Floating point operations: echo "scale=10; 22/7" | bc • tr '[a-z]' '[A-Z]' < file.txt – change lowercase to uppercase
1. Interactive control • $0, ${0} – name of script/executable
• awk '/pattern/ {action }' file – process lines containing pattern
Action set -o vi set -o emacs
• $1 to $9, ${1} to ${255} – positional command line parameters 8. Screen output • cut -d delimiter -f field – print column(s)
• PAR=${1:?"Missing parameter"} – error when ${1} is not set • echo "text" – print text, echo * print all files in current dir
vi-command mode (C) Esc —
• PAR=${1:-default} – when ${1} is not set, use default value • cmp file1 file2 – compare files and print first difference
Previous/next command in history Ctrl+p / Ctrl+n • echo -e "text" – interpret escape-sequences (\t tab., \a beep, \f new • diff, diff3, sdiff, vimdiff – compare whole files
jC / kC • $# – number of command line parameters (argc)
PageUp / PageDown page, \n new line), -n, \c suppressing \n, \xHH hex-byte, \nnn oct. byte, • dd if=in of=out bs=k count=n – read 𝑛 blocks of 𝑘 bytes
Automatic fill of file name Esc EscC Tab • ${!#} – the last command line parameter \u03B1 „𝛼“ (U+03B1) in UTF-8
• $* – expand all parameters, "$*" = "$1 $2 $3…" • strings – show printable strings in binary file
List of all matches Esc = Tab Tab • stty – change and print terminal line settings • paste file1 file2 – merge lines of files
Horizontal move in command line hC / lC Ctrl+b / Ctrl+f, ← / → • $@ – expand all parameters, "$@" = "$1" "$2" "$3"… • tty – print name of terminal connected to stdout
• $_ – last parameter of previous command • rev – reverse every line
Jump to line begin/end ^C / $C Ctrl+a / Ctrl+e • printf format values – format output
Backward/forward history search /C / ?C Ctrl+r / Ctrl+s • shift – rename arguments, $2 to $1, $3 to $2, etc.; lower counter $# • printf -v variable form. val. – form. output into variable 13. Search
Delete word to the end/begin dwC / dbC Esc d / Esc h • xargs command – read stdin and put it as parameters of command – % [flags][width][.precision][length]specifier • whereis, which – find path to command
Delete text from cursor to the line
d$C / d^C Ctrl+k / Ctrl+u 4.1. Read options from command line – Specifier: %u, %d, %i decimal; %E, %f float, %x, %X hex; %o octal, %s string, %% char % • grep – -i ignore case, -n print line number, -v display everything except
end/begin while getopts "a:b" opt; do case $opt in – Width: 𝑛 prints at least 𝑛 chars, spaces from right, 0𝑛 print at least 𝑛 chars, zeros pattern, -E extended regexp
a) echo a = $OPTARG ;; from left, * width specified in preceding parameter • locate file – find file
1.1. Command line history b) echo b ;; – Precision: min. number of digits, digits after decimal point, number of printed • find path -name 'file*' – search for file*
• history, fc -l – display numbered history of commands \?) echo "Unknown parameter!" ;; chars, * number of chars given by preceding parameter • find path -exec grep text -H {}\; – find file containing text
• !n – run command number n – Flags: - left-justify, + prints number with sign +/-
• !p – run last command beginning by p
esac; done
shift $(($OPTIND - 1)); echo "Last: $1" • printf "%d" \'A – display ASCII code of char “A” (65)
14. Users and permissions
• !! – repeat last entered command • printf \\$(printf '%03o' 65) – print char given by ASCII code • whoami, who am i – tell who I am :)
• !!:n – expand n-th parameter of last command 5. Control expressions • tput action – terminal dependent action • w, who, users, pinky, finger – list connected users
• !$ – expand the last parameter of last command • (commands), $(commands), `commands`, {commands;} – run in subshell • reset, tput sgr0, tset – reset terminal, cancel attributes • last / lastb – history successful / unsuccessful logins
• fc – run defined $EDITOR wit last command • $(program), `program` – output of program replaces command • clear, tput clear – clear screen • logout, Ctrl+d – exit shell
• fc -e vim z k – open vim editor with commands from 𝑧 to 𝑘 • test, [ ] – condition evaluation: • su login – change user to login
• ^old^new – substitute old with new in last command 9. Process management • sudo – run command as other user
– numeric comparison: a -eq b …𝑎 = 𝑏, a -ge b …𝑎 ≥ 𝑏, a -gt b …𝑎 >
• program `!!` – use output of last command as input 𝑏, a -le b …𝑎 ≤ 𝑏, a -lt b …𝑎 < 𝑏 • command & – run command in background • su - login -c 'command' – run one command as login
– file system: -d file is directory, -f file exists and is not dir., -r • prog1 && prog2 – run prog2, if prog1 ends with success • id login, groups login – show user details
1.2. Help and manuals • prog1 || prog2 – rub prog2, if prog1 ends with error • useradd, userdel, usermod – create, delete, edit user
file exists and is readable, -w file exists and is writable, -s file is
• type -a command – information about command • Ctrl+z – stop process (SIGSTOP) • groupadd, groupdel, groupmod – create, delete, edit group
non-zero size, -a file exists
• help command – brief help on bash command • bg / fg – run last stopped process in background/foreground • passwd – change password
– logical: -a and, -o or, ! negation
• man command, info command – detailed help • jobs – list processes running in background • pwck – check integrity of /etc/passwd
• [[ ]] – comparison of strings, equal =, non-equal !=, -z string is zero
• man -k key, apropos key, whatis key – find command • exec command – shell is replaced by command • chown user:group file – change owner, -R recursion
sized, -n string is non-zero sized, <, > lexical comparison
2. Debugging • [ condition ] && [ condition ] • wait – wait for end of background tasks • chgrp group file – change group of file
• true – returns 0 value • top – watch CPU, memory, system utilization • chmod permissions file – change permissions in octal of user, group, oth-
Run a script as: bash option script and its parameters • ps -xau – list processes and users, ps -xaf, pstree tree listing ers; 444=-r--r--r--, 700=-rwx------, 550=-r-xr-x---
• bash -x – print commands before execution • false – returns 1 value
• break – terminates executed cycle • pgrep process, pidof process – get PID by name of process • runuser login -c "command" – run command as user
• bash -u – stop with error if undefined variable is used • nice -n p command – priority 𝑝 od −20 (max.) to 19 (min.)
• bash -v – print script lines before execution • continue – starts new iteration of cycle
• renice -n p -p pid – change priority of running process
15. System utilities
• bash -n – do not execute commands • eval parameters – executes parameters as command • uname -a, cat /proc/version – name and version of operating system
• exit value – terminates script with return value • kill -s k n – send signal 𝑘 to proces id. 𝑛, 0, 1 SIGHUP; 2 SIGINT Ctrl+c;
3. Variables, arrays and hashes 3 SIGQUIT; 9 SIGKILL; 15 SIGTERM; 24 SIGSTOP • uptime – how long the system has been running
• . script, source script – reads and interprets another script • fuser – identify processes using files or sockets
• NAME=10 – set value to variable $NAME, ${NAME} • : argument – just expand argument or do redirect • trap 'command' signals – run command when signal received
• killall name – send signals to process by name • lsof – list open files
• export NAME=10, typedef -x NAME – set as environment variable • alias name='commands' – expand name to commands • sync – flush file system buffers
• D=$(date); D=`date` – variable contains output of command date • unalias name – cancel alias • nohup command & – command will continue after logout
• time command – print time of process execution • chroot dir command – run command with special root directory
• env, printenv – list all environment variables • if [ condition ]; then commands; • strace,ltrace program – show used system/library calls
• set – list env. variables, can set bash options and flags shopt elif [ condition ]; then commands; • times – print user and system time utilization in current shell
• watch -n s command – every s seconds run command • ldd binary – show library dependencies
• unset name – destroy variable of function else commands; fi
• typeset, declare – set type of variable • for variable in arguments; do commands; done • timeout N command – quit command after N seconds 15.1. Disk partitions
• readonly variable – set as read only – {a..z} – expands to a b c …z 10. Time and process planning • df – display free space
• local variable – set local variable inside function – {i..n..s} – sequence from i to n with step s • date – print date, date --date=@unix_time • mount – print mounted partitions
• ${!var}, eval \$$var – indirect reference – \"{a,b,c}\" – expands to "a" "b" "c" • date +"%Y%m%d %H:%M:%S %Z" – format to 20130610 13:39:02 CEST • mount -o remount -r -n / – change mount read only
• ${parameter-word} – if parameter has value, then it is used, else word is – {1,2}{a,b} – expands to 1a 1b 2a 2b • printf '%(%Y-%m-%d %H:%M:%S)T\n' – format date via printf %T • mount -o remount -w -n / – change mount writeable
used – seq start step end – number sequence • cal – display calendar • mount -t iso9660 cdrom.iso /mnt/dir -o loop – mount image
• ${parameter=word} – if parameter has no value assing word. Doesn’t work • for((i=1; i<10; i++)); do commands; done • crontab -e – edit crontab, -l list, format min hour date month day com- • mount -t cifs \\\\server\\ftp /mnt/adr -o user=a,passwd=b
with $1, $2, ets. • while returns true; do commands; done mand, * * * * * command run every minute, 1 * * * * command 1st min • umount partition – unmount partition
• ${parameter:-word} – works with $1, $2, etc. • until [ test returns true ]; do commands; done of every hour • fdisk -l – list disk devices and partitions
• ${parameter?word} – if parameter has value, use it; if no display word and • case $prom in value1 ) commands ;; • at, batch, atq, atrm – queue, examine or delete jobs for later execution • blkid – display attributes of block devices
exit script. value2 ) commands ;; *) implicit. commands ;; • tune2fs – change ext2/3/4 filesystem parameters
• ${parameter+word} – if parameter has value, use word, else use empty esac 11. File operations • mkfs.ext2, mkfs.ext3 – build file-system
string • Function definition: function name () {commands; } File name wildchars: ? a char; * zero or more chars; [set] one or more given • hdparm – set/read parameters of SATA/IDE devices
• array=(a b c); echo ${array[1]} – print ‘b’ • return value – return value of the function chars, interval [0-9] [a-z], [A-Z]; [!set], [^set] none of chars.
• array+=(d e f) – append new item/array at the end • declare -f function – print function declaration • ls – list directory, ls -la, vdir all files with info
15.2. System utilization
• ${array[*]}, ${array[@]} – all items of array • tree – display hierarchy tree of directories • ulimit -l – print limits of system resources
• ${#array[*]}, ${#array[@]} – number of array items
6. Redirections and pipes • file file – determine file by its magic number • free, vmstat – display usage of physical, virt. memory
• declare -A hash – create associative array (from version) • 0 stdin/input, 1 stdout/output, 2 stderr/error output • lsattr, chattr – list and change file attributes for ext2,3 • lspci, lsusb – list PCI, USB devices
• hash=([key1]=value ["other key2"]="other value") – store items • > file – redirection, create new file or truncate it to zero size • umask – define permission mask for new file • dmesg – display messages from kernel
• ${hash["other key2"]}, ${hash[other key2]} – access • >> file – append new data at the end of file • pwd (-P) – logical (physical) path to current directory • sysctl – configure kernel parameters at runtime
• ${hash[@]}, ${hash[*]} – all items • command1<<<command2 – ouput from 2nd to stdin of 1st • cd directory – change directory, cd jump to $HOME, cd - to $OLDPWD • dmidecode – decoder for BIOS data (DMI table)
• ${!hash[@]}, ${!hash[*]} – all keys • command < file – read stdin from file • dirs – list stack of directories • init, telinit – command init to change runlevel
• tee file – read stdin, writes to file and to stdout • pushd directory – store directory to stack • runlevel, who -r – display current runlevel
3.1. Strings • command 2> file – redirect error messages to file
• STRING="Hello" – indexing: H0 e1 l2 l3 o4 • exec 1> >(tee -a log.txt) – redirect stdout also to file
• popd – set top stack directory as actual directory 16. Networking
• cp source target – copy file
• STRING+=" world!" – concatenate strings • 2>&1 – merge stderr and stdout • hostname – display computer hostname
• ln -s source link – create a symbolic link
• ${#string}, expr length $string – string length • exec 3<>/dev/tcp/addr/port – create descriptor for network read/write • ping host – send ICMP ECHO_REQUEST
• mkdir, rmdir – create, remove directory
• ${string:position} – extract substring from position • exec 3>&- – close descriptor • dhclient eth0 – dynamically set eth0 configuration
• rm file, rm -r -f directory, unlink – delete
• ${string:position:length} – extract substr. of length from position • command > /dev/null 2>&1 – suppress all output • host, nslookup host/adr – DNS query
• touch file – create file, set actual time to existing file
• ${string/substring/substitution} – substitute first occurrence • n> n>> n>&m – operation redirect for descriptors n, m • dig – get record from DNS
• du -h – display space usage of directories
• ${string//substring/substitution} – substitute all • mkfifo name – make a named pipe, it can be written and read as file • whois domain – finds owner of domain or network range
• stat file – file statistics, stat --format=%s size
• ${string/%substring/substitution} – substitute last occurrence • command1 | command2 – pipe, connection between processes • ethtool eth0 – change HW parameters of network interface eth0
• basename name suffix – remove path or suffix
• ${string#substring} – erase shortest substring • command 2>&1 | . . . – can be shortened to command |& . . . • ifconfig – display network devices, device configuration
• dirname /path/to/file – print only path
• ${string##substring} – erase longest substring • ${PIPESTATUS[0]}, ${PIPESTATUS[1]} – retvals before and after pipe • ifconfig eth0 add 10.0.0.1 netmask 255.255.255.0
• repquota – summarize quotas for a filesystem
• read parameters – read input line and separate it into parameters • ifconfig eth0 hw ether 01:02:03:04:05:06 – change MAC address
3.2. Embedded variables • mktemp – create file with unique name in /tmp
• route add default gw 10.0.0.138 – set network gateway
• ~, $HOME – home directory of current user 6.1. Input for interactive programs (here documents) 12. Work with file content • route -n, netstat -rn – display route table
• $PS1, $PS2 – primary, secundary user prompt ./program << EOF ./program <<-'EOF' # suppress tabulators
• cat – concatenate files and print them to stdout • netstat -tlnp – display processes listening on ports
• $PWD, ~+ / $OLDPWD, ~- – actual/previous directory Input1 Input1
• cat > file – create file, end with Ctrl+d • arp – display ARP table
• $RANDOM – random number generator, 0 – 32,767 Input2 Input2
• mapfile A < file – store stdin into array $A • iptables -L – display firewall rules
• $? – return value of last command EOF EOF
• tac – like cat, but from bottom to top line • tcpdump -i eth0 'tcp port 80' – display HTTP communication
• $$ – process id. of current process 6.2. Process file line by line • more, less – print by pages, scrollable • tcpdump -i eth0 'not port ssh' – all communication except SSH
• $! – process id. of last background command cat file.txt | (while read L; do echo "$L"; done) • od, hexdump -C, xxd – print in octal, hex dump • ssh user@hostname command – run command remotely
• $PPID – process id. of parent process • wc – get number of lines -l, chars -n, bytes -c, words -w • mail -s "subject" address – send email to address
• $- – display of bash flags 7. Evaluating mathematical expressions • wget -e robots=off -r -L http://path – mirror given page