---
# System prepended metadata

title: L5. Bash Scripting. Part 4

---

# L5. Bash Scripting. Part 4

## Standard file descriptors
:::info
0 - STDIN - <file
1 - STDOUT - >file
2 - STDERR - 2>file
:::

## Redirecting errors

```bash=
ls –l myfile xfile anotherfile 2> errorcontent 1> correctcontent
ls –l myfile xfile anotherfile &> content
```

---

## Output redirection
* Temporarily redirection
* Permanently redirection


#### Temporarily redirection
```bash=
#!/bin/bash
echo "Error message" >&2
echo "Normal message"
```
```bash=
./myscript 2> myfile
```

#### Permanent redirections
```bash=
#!/bin/bash
exec 1>outfile
echo "Permanent redirection"
echo "from a shell to a file."
echo "without redirecting every line"
```
```bash=
./myscript
cat ./outfile
```
```bash=
#!/bin/bash
exec 2>myerror
echo "Script Begining ..."
echo "Redirecting Output"
exec 1>myfile
echo "Output goes to the myfile"
echo "Output goes to myerror file" >&2
```

## Redirecting input
```bash=
exec 0< myfile
```
>testfile
:::info
first line
second line
third line
fourth line
:::
```bash=
#!/bin/bash
exec 0<testfile
total=1
while read line; do
	echo "#$total: $line"
	total=$(($total + 1))
done
```

---

## Suppressing command output
```bash=
ls -al badfile anotherfile 2> /dev/null
```
