Useful Shell Recipes

Last updated on 2015-04-25 12:09:48 -0300

1. For Loop

for i in {1..10}
do
    echo $i
done

N=10
for i in `seq 1 $N`
do
    echo $i
done

2. Create Text File Interactively

$ cat > foo
Enter
Text
Here
<Ctrl+D>
$

3. Create Text File From Script

cat > foo << EOF
Enter
Text
Here
EOF

4. Read Stream Line by Line

while read line
do
    echo $line
done < stream

5. Named Pipe (a.k.a. FIFO)

$ mkfifo my_pipe
$ gzip -9 -c < my_pipe > out.gz &
$ cat file > my_pipe
$ rm my_pipe