Update 'posix_shell.md'

master
Jan Danielzick 2022-09-22 18:30:36 +00:00
parent 489c8fbeee
commit 77770b3b35
1 changed files with 47 additions and 15 deletions

View File

@ -1,4 +1,4 @@
#Shell Cheatsheet
# Shell Cheatsheet
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.
@ -15,43 +15,73 @@ 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.
If you are trying to learn how to use the shell, I encourage you to 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
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.
##Shell features:
## Shell features:
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.
**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...).
**Streams**
Normally, there are two output streams and one input stream for a running program:
Standard output (stdout), standard error (stderr), 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,
which will end up in your terminal when you run a shell interactively.
There are two notable exceptions to this which you can specify:
1. Reditects: 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
the standard output/error streams.
`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.
- `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.
The most common example of this is redirecting stderr to stdout and
the combined stdout into a file like so: `COMMAND > FILE 2>&1`.
2. Pipes: The output of one command can be used as the input of another:
**Pipes**: The output of one command can be used as the input of another:
`COMMAND | OTHER_COMMAND`
**Variables**
There are two kinds of
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`.
exit code
if, esle
while, for
@ -59,14 +89,16 @@ pipes, redirects
ctrl-c
*Builtin commands:*
- `cd` [change directoy] A shell (or any process for that matter) is always working from a specific working directory. Pretty much self-explanatory what this does.
- `exit` exit (optionally with an exit code though that feature is more useful for scripts)
- `export` make the given shell variable an environment variable
- `unset` unset a variable
- `jobs` show programs that are currently running in the background
- `fg` get a program from the background in the foreground
wait
__Basic commands:__
- `sh`
- `export`
- `unset`
- `cd` [change directory] (see "Working Directory" section)
- `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.