Newer
Older
Bash 4.3 comes with 58 embeded commands (built-in):
```
bash defines the following built-in commands: :, ., [, alias, bg, bind, break, builtin,
case, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval,
exec, exit, export, fc, fg, getopts, hash, help, history, if, jobs, kill, let, local,
logout, popd, printf, pushd, pwd, read, readonly, return, set, shift, shopt, source,
suspend, test, times, trap, type, typeset, ulimit, umask, unalias, unset, until, wait,
while.
```
There are many more commands available on your machine (e.g. `ls`). Unix machines have several hundreds of different commands.
A good place to look at them is ([http://ss64.com/mac](http://ss64.com/mac) or [http://ss64.com/bash](http://ss64.com/bash).
!!! note
To check if a command is a `built-in` command run `type command`.
Try `type cd` and `type ls`
**No worries! As most of people we only need to use a very small subset of those commands.**
## How commands works
```bash
command [ -option ] [ arguments ]
```
* [ ] means this is optional (e.g. `ls` vs `ls -l`)
* **Option** only modify the behavior of the command. It starts with `-` (or `--` for long option)
* **Argument** controls the output of the command. It specifies a target for the command.
It is possible to save the output of a command in a variable:
```bash
var=$(command)
```
## Getting help
There are several ways to get help for a command in Bash:
| Command | Works For | Example | Notes |
|-------------------------|---------------------------------|----------------|-------|
| `man <command>` | Most external commands | `man ls` | Doesn't work for shell built-ins like `cd` |
| `<command> --help` | Many external commands | `ls --help` | Some commands may not support `--help` |
| `info <command>` | Commands with info pages | `info grep` | Not all commands have an info page |
| `help <command>` | Shell built-ins | `help cd` | Works only for built-in commands like `cd`, `exit` |
## String Commands Together
It is possible to execute several commands in one line using `;` in between commands:
It is possible to send the output (STDOUT) of a command as input (STDIN) of another using `|` :
## Input/Output of commmands
### A stream
In Bash, a stream is a flow of data used for input and output operations. Streams allow data to be read from or written to files, commands, or devices.
Streams are essential for scripting, allowing flexible input/output handling and error management.
echo "Papa" | tr Pp Mm
Bash primarily deals with three standard streams:
#### Standard Input (stdin) – File Descriptor 0
It is used to take input (e.g., from the keyboard or a file).
To read user input from keyboard:
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
read var
```
To read input from a file:
```bash
command < input.txt
```
#### Standard Output (stdout) – File Descriptor 1
It is used to display _normal_ command output.
When you do `echo "Hello"` the ouput is written to stdout.
Stdout can be redirected to a file using `>` or another command using `|`.
```bash
command > output.txt # redirection to a file
command1 | command2 # redirection to the next command
```
#### Standard Error (stderr) – File Descriptor 2
It is used for error messages and diagnostics.
```bash
ls nonexistentfile
```
By default, the terminal (stdout) displays both file descriptor 1 (stdout) and file descriptor 2 (stderr).
You can redirect specificaly `stderr` in a dedicated file using the file descriptor 2:
```bash
# test1
ls nonexistentfile 2> error.log
# test2
ls . nonexistentfile 2> error.log
# test3
ls . nonexistentfile > output.txt 2> error.txt
### Commands specificities
/!\\ commmands that take an input either from a **file** of from **STDIN**: grep, sed, cat, head, sort, wc, etc.
/!\\ commmands that **never read STDIN**: ls, cp, mv, date, who, pwd, echo, cd, etc.
/!\\ commmands that **read only STDIN**: tr
## Saving output
It is possible to save/send the output of a command in a file:
```bash
# overwrite file if already exists
command > file
# append file if already exists
command >> file
```
!!! Success "Quick recap"
In this section we've learned:
- how commands work
- how to string commands together
- how to deal with input/output of commands
- the different file descriptors (stdout, stdin, stderr)