Jordi Pla

Shell Recipes

Basic

For loop:

N=10
for i in $(seq 1 ${N}); do
	echo ${i}
done

Count files in the current directory:

ls -l | wc -l

Infinite loop:

while :; do
	echo "Looping..."
done

Multithreading:

sleep 1 &
echo "Hello" &
wait

Create foo text file from script:

cat > foo << EOF
Some
Text
Here
EOF

Read line by line:

while read line; do
	echo ${line}
done < INPUT

Create a FIFO (named pipe):

mkfifo named_pipe
ls > named_pipe &
cat < named_pipe
rm named_pipe