Update 'posix_shell.md'
parent
77770b3b35
commit
5e7d94a3e6
193
posix_shell.md
193
posix_shell.md
|
@ -1,11 +1,13 @@
|
||||||
# Shell Cheatsheet
|
# Shell Cheatsheet
|
||||||
|
|
||||||
I am only going over POSIX shell things here as there are a bunch
|
## Introduction
|
||||||
of extensions found in some shells / on some systems but not others.
|
|
||||||
|
I am only going over POSIX shell things here as there are a bunch of extensions
|
||||||
|
found in some shells / on some systems but not others.
|
||||||
The sections below should apply to any UNIX-like system.
|
The sections below should apply to any UNIX-like system.
|
||||||
This cheatsheet will by no means be a comprehensive guide to UNIX
|
This cheatsheet is by no means a comprehensive guide to UNIX shell scripting
|
||||||
(it’s a cheatsheet, not a handbook) but you can always find out
|
(it’s a cheatsheet, not a handbook, and I don’t know everything) but you can usually
|
||||||
more information about any given command right there on the shell.
|
find out more information about any given command right there on the shell.
|
||||||
|
|
||||||
Many systems come with manuals, try `man COMMAND` or
|
Many systems come with manuals, try `man COMMAND` or
|
||||||
`info COMMAND` to find out more about most utilities
|
`info COMMAND` to find out more about most utilities
|
||||||
|
@ -14,6 +16,7 @@ Alternatively, if no manual is available,
|
||||||
passing the flag `-h` or `--help` will usually print some information.
|
passing the flag `-h` or `--help` will usually print some information.
|
||||||
I find myself consulting these constantly because nobody is expected
|
I find myself consulting these constantly because nobody is expected
|
||||||
to remember everything beyond the basics and it’s usually faster than googling it.
|
to remember everything beyond the basics and it’s usually faster than googling it.
|
||||||
|
That said, googling for a solution is generally also a viable option.
|
||||||
|
|
||||||
If you are trying to learn how to use the shell, I recommend you start
|
If you are trying to learn how to use the shell, I recommend you start
|
||||||
with the examples section at the end and work your way backwards from there
|
with the examples section at the end and work your way backwards from there
|
||||||
|
@ -22,7 +25,7 @@ I also highly recommend you check the manuals or help information for
|
||||||
any command you want to use - especially if it was given to you
|
any command you want to use - especially if it was given to you
|
||||||
by a stranger on the internet.
|
by a stranger on the internet.
|
||||||
|
|
||||||
## Shell features:
|
## Shell Features
|
||||||
|
|
||||||
The shell’s main tasks are providing a text based user interface to an
|
The shell’s main tasks are providing a text based user interface to an
|
||||||
operating system as well as running scripts.
|
operating system as well as running scripts.
|
||||||
|
@ -42,14 +45,14 @@ Relative paths are just the equivalent of an absolute path with the working dire
|
||||||
In many cases, just the name of a file or subdirectory inside the working directory is used.
|
In many cases, just the name of a file or subdirectory inside the working directory is used.
|
||||||
|
|
||||||
You can change to another working directory using `cd` (change directory) like so: `cd PATH_TO_DIRECTORY`.
|
You can change to another working directory using `cd` (change directory) like so: `cd PATH_TO_DIRECTORY`.
|
||||||
When no path is specified, `cd` will take you to the current user’s home directory (usually...).
|
When no path is specified, `cd` takes you to the current user’s home directory (usually...).
|
||||||
|
|
||||||
**Streams**
|
**Streams**
|
||||||
|
|
||||||
Normally, there are two output streams and one input stream for a running program:
|
Normally, there are two output streams and one input stream for a running program:
|
||||||
Standard output (stdout), standard error (stderr), and standard input (stdin).
|
Standard output (stdout), standard error (stderr), and standard input (stdin).
|
||||||
By default, they are passed to the stdout, stderr, and stdin of the shell,
|
By default, they are passed to the stdout, stderr, and stdin of the shell,
|
||||||
which will end up in your terminal when you run a shell interactively.
|
which end up in your terminal when you run a shell interactively.
|
||||||
There are two notable exceptions to this which you can specify:
|
There are two notable exceptions to this which you can specify:
|
||||||
|
|
||||||
**Redirects**: You can send the output of a command to a file instead of
|
**Redirects**: You can send the output of a command to a file instead of
|
||||||
|
@ -82,58 +85,146 @@ They can be added to the environment of subsequent commands using `export VARIAB
|
||||||
To alter the environment for just one command, prepend it with the variable like so: `VARIABLE=VALUE COMMAND`.
|
To alter the environment for just one command, prepend it with the variable like so: `VARIABLE=VALUE COMMAND`.
|
||||||
Variables can be unset using `unset VARIABLE`.
|
Variables can be unset using `unset VARIABLE`.
|
||||||
|
|
||||||
exit code
|
Unknown or unset variables are ignored.
|
||||||
if, esle
|
|
||||||
while, for
|
|
||||||
pipes, redirects
|
|
||||||
ctrl-c
|
|
||||||
|
|
||||||
*Builtin commands:*
|
**Control Flow**
|
||||||
- `exit` exit (optionally with an exit code though that feature is more useful for scripts)
|
|
||||||
- `jobs` show programs that are currently running in the background
|
|
||||||
- `fg` get a program from the background in the foreground
|
|
||||||
wait
|
|
||||||
|
|
||||||
__Basic commands:__
|
When a command finishes running, it produces an exit code that gives some rudimentary status information
|
||||||
- `sh`
|
Usually, an exit code of 0 indicates success and anything else an error of some kind.
|
||||||
- `export`
|
|
||||||
- `unset`
|
This can be used in control structures (while loops, conditions) or directly accessed using the special
|
||||||
- `cd` [change directory] (see "Working Directory" section)
|
shell variable `$?`.
|
||||||
- `pwd` [print working directory] self-explanatory
|
|
||||||
- `ls` [list] Get a listing of the specified directory. If none given, the current directory is used.
|
If statements are built as follows:
|
||||||
- `cat` [concatenate] Originally created to concatenate files, it is most commonly used to print the contents of a file to the standard output. It takes file paths as arguments.
|
```sh
|
||||||
- `mv` [move] Move or rename a file or directory. It takes two arguments: Origin and destination.
|
if COMMAND; then
|
||||||
- `cp` [copy] Copy a file or directoy. It takes two arguments: Origin and destination.
|
OTHER
|
||||||
- `touch`
|
COMMANDS
|
||||||
|
HERE
|
||||||
|
else
|
||||||
|
MORE
|
||||||
|
COMMANDS
|
||||||
|
HERE
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
There are also two operators that can be used as short forms of if and else: `&&` and `||`.
|
||||||
|
They can be combined and chained as needed.
|
||||||
|
Commands that are chained using `&&` only run if the previous command exited with exit code 0.
|
||||||
|
Commands that are chained using `||` run if the previous command *didn’t* exit with exit code 0.
|
||||||
|
In many cases, this is used as follows: `COMMAND && THEN_COMMAND || ELSE_COMMAND`
|
||||||
|
|
||||||
|
|
||||||
|
While loops are built as follows:
|
||||||
|
```sh
|
||||||
|
while COMMAND; do
|
||||||
|
OTHER
|
||||||
|
COMMANDS
|
||||||
|
HERE
|
||||||
|
done
|
||||||
|
```
|
||||||
|
The `[ ... ]` seen in many if statements and while loops is not actually part of the shell syntax
|
||||||
|
but an alias to the `test` command (see below).
|
||||||
|
|
||||||
|
For loops are different in that they act as "for each" instead of checking a condition.
|
||||||
|
```sh
|
||||||
|
for VARNAME in STRING; do
|
||||||
|
COMMANDS
|
||||||
|
USING $VARNAME
|
||||||
|
HERE
|
||||||
|
done
|
||||||
|
```
|
||||||
|
You can break out of a loop using `break`.
|
||||||
|
|
||||||
|
You can end the shell session or script using `exit`. It optionally takes an exit code as argument.
|
||||||
|
|
||||||
|
**Job Control**
|
||||||
|
|
||||||
|
You can send a process to the background by adding an `&` at the end of the command.
|
||||||
|
The `jobs` command shows programs that are currently running in the background.
|
||||||
|
You can get a process from the background into the foreground using `fg`.
|
||||||
|
Alternatively, if you just want to wait for all jobs to finish, `wait` does that.
|
||||||
|
|
||||||
|
**Miscellaneous**
|
||||||
|
|
||||||
|
Spaces separate arguments and newlines separate commands. To override this, use quoted strings.
|
||||||
|
Single-quoted strings are taken verbatim, variables in double-quoted strings get replaced.
|
||||||
|
|
||||||
|
The POSIX shell is case-sensitive but it’s not whitespace-sensitive meaning you can put
|
||||||
|
multiple whitespace characters and they count as one or get ignored.
|
||||||
|
You can put multiple commands on the same line using semicolons.
|
||||||
|
This is commonly used for formatting as it makes scripts more readable.
|
||||||
|
|
||||||
|
The built-in `read` command can be used in scripts to read input into a variable.
|
||||||
|
|
||||||
|
Command line arguments given to a script end up in the variables `$0` through `$9`.
|
||||||
|
They can be moved to the left using `shift`. `$0` starts out being the name of the script itself.
|
||||||
|
|
||||||
|
You can send Ctrl+c to the terminal to send an interrupt to the process running inside it.
|
||||||
|
Note that this is a feature of the terminal, not of the shell itself.
|
||||||
|
Usually, this is used to stop whatever child process of the shell is running in the foreground.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
This section gives you a list of commonly used UNIX utilities.
|
||||||
|
This does not include shell-builtins as I have covered them above already.
|
||||||
|
Some shells have built-in versions of some of these commands but they should mostly work
|
||||||
|
the same as their dedicated counterparts.
|
||||||
|
|
||||||
|
- `sh` (shell) The shell itself, can also be used to invoke scripts
|
||||||
- `echo` output text to standard output
|
- `echo` output text to standard output
|
||||||
- `test` [more commonly known as `[ ... ]`] used to check whether a condition is true or false, result is passed back using exit code
|
- `pwd` (print working directory)
|
||||||
- `df` [disk free] show available disk space
|
- `ls` (list) Get a listing of the specified directory. If none given, the current directory is used.
|
||||||
- `du` [disk usage] show how much space a given file or directoy is using
|
- `cat` (concatenate) Originally created to concatenate files, it is most commonly used to print
|
||||||
- `su` [switch user] self-explanatory
|
the contents of a file to the standard output.
|
||||||
- `rm` [remove] remove a file or directoy
|
- `mkdir` (make directory) self-explanatory
|
||||||
- `mkdir` [make directory] self-explanatory
|
- `touch` Change the access and modification timestamp of a file. Also commonly used to create empty files.
|
||||||
- `find` find a file or directoy by specified criteria
|
- `mv` (move) files or directories
|
||||||
|
- `cp` (copy) files or directories
|
||||||
|
- `rm` (remove) files or directories
|
||||||
|
- `test` (more commonly known as `[ ... ]`) used to check conditions, result is passed back using exit code
|
||||||
|
- `true` returns exit code 0
|
||||||
|
- `false` returns non-zero exit code
|
||||||
- `grep` search for regular expressions in text
|
- `grep` search for regular expressions in text
|
||||||
- `ps`
|
- `tr` (text replace) replace all occurrences of a character in text
|
||||||
- `id`
|
- `sed` (stream editor) edit text on the fly
|
||||||
- `uname` [UNIX name] get information about the operating system - usually name, architecture, and build information
|
- `uname` (UNIX name) get information about the operating system
|
||||||
- `chmod` [change mode] change file permissions
|
- `id` (identity) get information about users
|
||||||
- `chown` [change owner] change file ownership
|
- `su` (switch user)
|
||||||
- `sed` [stream editor] edit text on the fly
|
- `ps` (process status) get information about running processes
|
||||||
- `vi` unholy abommination of a text editor :)
|
|
||||||
- `kill` kill a given process
|
- `kill` kill a given process
|
||||||
- `less` or `more` print only a screen worth of text at a time and wait so you have the time to read it
|
- `find` find a file or directoy by specified criteria
|
||||||
|
- `chmod` (change mode) change file permissions
|
||||||
|
- `chown` (change owner) change file ownership
|
||||||
|
- `df` (disk free) show available disk space
|
||||||
|
- `du` (disk usage) show how much space a given file or directoy is using
|
||||||
|
- `vi` unholy abommination of a text editor :)
|
||||||
|
- `more` print only a screen worth of text at a time and wait so you have the time to read it
|
||||||
|
|
||||||
__Commonly used special files__
|
## Commonly Used Special Files
|
||||||
. ..
|
|
||||||
dev null
|
- `.` The working directory
|
||||||
dev urandom
|
- `..` Parent directory of the working directory
|
||||||
|
- `/dev/null` Your personal trash can to redirect all the output you don’t care about to.
|
||||||
|
Also known as a black hole or the indefinite void. :)
|
||||||
|
- `/dev/urandom` Source of infinite random data
|
||||||
|
- `/dev/zero` Source of infinite null bytes
|
||||||
|
|
||||||
|
## Honorable Mentions
|
||||||
|
|
||||||
__Honorable mentions:__
|
|
||||||
The following things won’t be available on all systems, but are really handy when they are:
|
The following things won’t be available on all systems, but are really handy when they are:
|
||||||
- `free` shows information about used/free RAM on Linux systems
|
|
||||||
|
- `less` better version of `more`
|
||||||
- `nano` nice user-friendly terminal text editor available on many systems
|
- `nano` nice user-friendly terminal text editor available on many systems
|
||||||
- `htop` nice user-friendly terminal task manager
|
- `htop` nice user-friendly terminal task manager
|
||||||
- `which` easy way to find out where the binary for a given command is located
|
- `which` easy way to find out where the binary for a given command is located
|
||||||
|
- `sudo` become root (or any other user) by authenticating the current user
|
||||||
|
instead of the target user given the current user is permitted to do so - useful when root login is disabled
|
||||||
|
- `free` shows information about used/free RAM on Linux systems
|
||||||
|
- `/dev/stdin`, `/dev/stdout`, and `/dev/stderr` special files for you guessed what
|
||||||
- `sl` a great way to infuriate anyone who happens to type too quickly
|
- `sl` a great way to infuriate anyone who happens to type too quickly
|
||||||
- `sudo` become root (or any other user) by authenticating the current user instead of the target user given the current user is permitted to do so - useful when root login is disabled
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
Below are some examples that you can use to better understand above material.
|
||||||
|
|
||||||
|
... to be finished another time ...
|
Loading…
Reference in New Issue