Unix

Kernel

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.

Shell

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.

Input/Output

  • standard input
  • standard output
  • standard error

Redirect

  • > - write to file
  • >> - append to file

Useful commands

Utilities

  • ls - list directory contents
  • cd - change directory
  • pwd - return working directory
  • mkdir - make directories
  • touch - change file access and modification times
  • cp - copy files
  • rm - remove directory entries
  • mv - move files
  • echo - write arguments to the standard output
  • xargs - construct argument list(s) and execute

Searching

  • find - walk a file hierarchy
  • locate - find filenames quickly
  • grep - file pattern searcher
  • tree list contents of directories in a tree-like format. Requires installation via a package manager such as homebrew.

Reading and manipulating files

  • cat - concatenate and print files
  • sort - sort or merge lines of text and binary files
  • sed - stream editor
  • awk - pattern-directed scanning and processing language

Job control

Interrupting 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 facilities
  • kill - terminate or signal a process
  • nohup - invoke a utility immune to hangups

Scripting

A 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.

Shebang

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.

Quotes

  • Single - prevent variable interpolation, etc..
  • Double - allow variable interpolation, etc..
  • Backticks - reserved for command substitution

Conditionals

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

Substitutions

Command substitution

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

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

Functions

name_of_func() {
  # first param is `$1`
  # second param is `$2`
  # etc.
}

# call the function
name_of_func "var_1_param" "var_2_param"

Relational operators

  • -eq - Equal to
  • -ne - Not equal
  • -lt - Less than
  • -gt - Greater than
  • -le - Less than or equal to
  • -ge - Greater than or equal to

Conditional expressions

Conditional 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 file

More at https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html

Resources

Tutorials

Tools