The kernel is the heart of the operating system. It interacts with the hardware and most of the tasks like memory management, task scheduling and file management.
The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.
>
- write to file>>
- append to filels
- list directory contentscd
- change directorypwd
- return working directorymkdir
- make directoriestouch
- change file access and modification timescp
- copy filesrm
- remove directory entriesmv
- move filesecho
- write arguments to the standard outputxargs
- construct argument list(s) and executefind
- walk a file hierarchylocate
- find filenames quicklygrep
- file pattern searchertree
list contents of directories in a tree-like format. Requires installation via a package manager such as homebrew.cat
- concatenate and print filessort
- sort or merge lines of text and binary filessed
- stream editorawk
- pattern-directed scanning and processing languageInterrupting a job while it is executing can be done using UNIX communication mechanisms called signals which provide software interrupts. For example, ^c
is a keybinding to SINGINT
.
You can also pause a job with ^z
, check to see it's in the background by running jobs
, and resume the job in the foreground with fg %<job-number>
or in the background with bg %<job-number>
.
signal
- simplified software signal facilitieskill
- terminate or signal a processnohup
- invoke a utility immune to hangupsA list of commands to be executed sequentially can be combined into a sigle file called a script. Syntax can be specific to bash, zsh, or any number of command-line interpreter scripting languages.
Run a shell script with the sh
command followed by the file path.
Adding a shebang to the beginning of a script and making it an executable by running chmod +x <file_name>
allows the file to be called without the sh
command. chmod -x
reverts the executable to a plain file.
Scripts begin with a shebang #!
followed by the absolute path to the interpreter /bin/<path_to_interpreter>
. You can also modify the shebang to ensure that the script is portable across systems by using /usr/bin/env <program>
. Read more about script portability.
if
statements check the exit code of commands. $?
returns the exit code of the most recently run command. An if
statement with the condition wrapped in square brackets checks if the condition renders true
.
if [ expression ]
then
# Statement(s) to be executed if condition is true
else
# Statement(s) to be executed if condition is false
fi
Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the place of the commands.
Syntax:
# older shells
`command`
# modern shells
$(command)
Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.
${var} # Substitute the value of var
More at https://www.tutorialspoint.com/unix/unix-shell-substitutions.htm
name_of_func() {
# first param is `$1`
# second param is `$2`
# etc.
}
# call the function
name_of_func "var_1_param" "var_2_param"
-eq
- Equal to-ne
- Not equal-lt
- Less than-gt
- Greater than-le
- Less than or equal to-ge
- Greater than or equal toConditional expressions are used by the [[
compound command and the test
and [
builtin commands.
-a
- true if file exists-d
- true if file exists and is a directory-e
- true if file exists-f
- true if file exists and is a regular fileMore at https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html