Skip to content
Snippets Groups Projects
bash-7-commands.md 2.28 KiB
Newer Older
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
# Commands

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
Bash 4.3 comes with 58 embeded commands:
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
```
 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.
```
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
They are built-in for performance reasons.  
There are many more commands available on your machine. 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).
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
**No worries! As most of people we only need to use a very small subset of those commands.**
jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed
## 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 execute several commands in one line using **;** in between commands:  

```bash
command1 ; command2
```

It is possible to save the output of a command in a variable:  

```bash
var=$(command)
```

### Input/Output of commmands

Commands can take different arguments:

** Nothing **  
```bash
ls
```

** String **  
```bash
echo hello world
```

** File **  
```bash
cat file_input
```

** A stream **  
What is a stream?
    explanation here

```bash
cat file_input | tr a-z A-Z
```

It is possible to send the output (STDOUT) of a command as input (STDIN) of another using **|**:  

```bash
command1 | command2
```

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
```

/!\\ 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

jacques.dainat_ird.fr's avatar
jacques.dainat_ird.fr committed

### Command pipes