Guides/posix_shell.md

139 lines
6.6 KiB
Markdown
Raw Normal View History

2022-09-22 20:30:36 +02:00
# Shell Cheatsheet
2022-09-22 19:33:15 +02:00
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.
This cheatsheet will by no means be a comprehensive guide to UNIX
(its a cheatsheet, not a handbook) but you can always find out
more information about any given command right there on the shell.
Many systems come with manuals, try `man COMMAND` or
`info COMMAND` to find out more about most utilities
(try `man man`, for example).
Alternatively, if no manual is available,
passing the flag `-h` or `--help` will usually print some information.
I find myself consulting these constantly because nobody is expected
to remember everything beyond the basics and its usually faster than googling it.
2022-09-22 20:30:36 +02:00
If you are trying to learn how to use the shell, I recommend you start
2022-09-22 19:33:15 +02:00
with the examples section at the end and work your way backwards from there
to figure out what they do.
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
by a stranger on the internet.
2022-09-22 20:30:36 +02:00
## Shell features:
2022-09-22 19:33:15 +02:00
The shells main tasks are providing a text based user interface to an
operating system as well as running scripts.
To facilitate this, the shell runs the programs the user/script specifies
and deals with the output.
2022-09-22 20:30:36 +02:00
**Working Directory**
A shell (or any process for that matter) is always running inside a specific directory
on the file system, the so-called working directory.
It is relevant when determining where files specified by relative path are located.
There are two kinds of file paths in the UNIX world: relative and absolute.
Absolute paths refer to a fixed location on the file system and start with a `/`.
Relative paths refer to a location relative to the current working directory and cannot start with a `/`.
Relative paths are just the equivalent of an absolute path with the working directory cut off.
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`.
When no path is specified, `cd` will take you to the current users home directory (usually...).
2022-09-22 19:33:15 +02:00
**Streams**
2022-09-22 20:30:36 +02:00
2022-09-22 19:33:15 +02:00
Normally, there are two output streams and one input stream for a running program:
2022-09-22 20:30:36 +02:00
Standard output (stdout), standard error (stderr), and standard input (stdin).
2022-09-22 19:33:15 +02:00
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.
There are two notable exceptions to this which you can specify:
2022-09-22 20:30:36 +02:00
**Redirects**: You can send the output of a command to a file instead of
2022-09-22 19:33:15 +02:00
the standard output/error streams.
2022-09-22 20:30:36 +02:00
- `COMMAND > FILE` redirects stdout of COMMAND into FILE.
- `COMMAND 2>FILE` redirects stderr into FILE.
- `COMMAND 2>&1` redirects stderr into stdout.
You can have multiple redirects per command though more than two usually dont make sense.
2022-09-22 19:33:15 +02:00
The most common example of this is redirecting stderr to stdout and
the combined stdout into a file like so: `COMMAND > FILE 2>&1`.
2022-09-22 20:30:36 +02:00
**Pipes**: The output of one command can be used as the input of another:
2022-09-22 19:33:15 +02:00
`COMMAND | OTHER_COMMAND`
**Variables**
2022-09-22 20:30:36 +02:00
There are two kinds of variables that you can deal with on the shell, lets call them
shell variables and environment variables. They behave the same when interacting with them
on the shell but shell variables are only available in the shell whereas environment variables
are handed to a new process when it is spawned.
The shell can set environment variables for its child processes and it itself has variables
from the environment it launched with. The environment it launched with is passed on
to the processes it spawns.
Shell variables can be set using `VARIABLE=VALUE`.
They can be added to the environment of subsequent commands using `export VARIABLE`.
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`.
2022-09-22 19:33:15 +02:00
exit code
if, esle
while, for
pipes, redirects
ctrl-c
*Builtin commands:*
- `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
2022-09-22 20:30:36 +02:00
wait
2022-09-22 19:33:15 +02:00
__Basic commands:__
2022-09-22 20:30:36 +02:00
- `sh`
- `export`
- `unset`
- `cd` [change directory] (see "Working Directory" section)
2022-09-22 19:33:15 +02:00
- `pwd` [print working directory] self-explanatory
- `ls` [list] Get a listing of the specified directory. If none given, the current directory is used.
- `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.
- `mv` [move] Move or rename a file or directory. It takes two arguments: Origin and destination.
- `cp` [copy] Copy a file or directoy. It takes two arguments: Origin and destination.
- `touch`
- `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
- `df` [disk free] show available disk space
- `du` [disk usage] show how much space a given file or directoy is using
- `su` [switch user] self-explanatory
- `rm` [remove] remove a file or directoy
- `mkdir` [make directory] self-explanatory
- `find` find a file or directoy by specified criteria
- `grep` search for regular expressions in text
- `ps`
- `id`
- `uname` [UNIX name] get information about the operating system - usually name, architecture, and build information
- `chmod` [change mode] change file permissions
- `chown` [change owner] change file ownership
- `sed` [stream editor] edit text on the fly
- `vi` unholy abommination of a text editor :)
- `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
__Commonly used special files__
. ..
dev null
dev urandom
__Honorable mentions:__
The following things wont be available on all systems, but are really handy when they are:
- `free` shows information about used/free RAM on Linux systems
- `nano` nice user-friendly terminal text editor available on many systems
- `htop` nice user-friendly terminal task manager
- `which` easy way to find out where the binary for a given command is located
- `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