Bash
Echo
- echo
- pwd
- man
echo Hello World
echo $BASH_VERSION
bash --version
echo and pwd are external programs that we can run using the shell
man bash
man cd (does not exist)
help cd
First Shell script
- echo
- chmod
- #!
chmod +x hello_world.sh
#!/usr/bin/env bash
echo "Hello World"
Bash command line parameters
- $1
#!/usr/bin/env bash
echo "Hello $1"
$1 $2 $3
$* which is $1 $2 $3 ... but that splits up the spaces that might be inside $1 or $2 ..
"$*" is the same
$@ is the same but
"$@" is "$1" "$2" "$3" ...
Bash if parameter
- if
#!/bin/bash
if [ "$1" == "" ]
then
echo $0 DIRNAME
exit 1
fi
echo "$1"
Bash while-loop
- while
while true; do (date; sleep 1); done
#!/usr/bin/env bash
while [[ -f /tmp/run ]]
do
echo "Running"
sleep 2
done
echo done
Bash for-loop
- for
#!/usr/bin/env bash
FRUITS="Apple Banana Peach Orange Strawberry"
for f in $FRUITS
do
echo $f
done
Bash for loop on a range of numbers
for ((i=1;i<=10;i++)); do echo $i; done
#!/usr/bin/env bash
for ((i=1;i<=10;i++));
do
echo $i;
done
Bash for loop on files
- for
#!/usr/bin/env bash
# Ynon
#FILES=examples/*
#for f in $FILES
#do
# echo "File $f"
#done
for f in examples/*
do
echo "File $f"
done
Bash for loop on command-line arguments
- $@
If there is no list to go over (there is no "in something" part) then by default it will go over
the values on the command line as if this was written:
for a in "$@"
#!/usr/bin/env bash
# Ynon
for a
do
echo "Argument $a"
done
# ./examples/for_arguments.sh a bb "c d"
Bash a sequence of number - a range of numbers
-
seq
-
seq FROM STEP TO
#!/usr/bin/env bash
seq 1 1 5
echo
seq 1 2 7
1
2
3
4
5
1
3
5
7
Arithmetic in Bash
$ x=2
$ echo $x
2
$ (( x = x + 3 ))
$ echo $x
5
$ ((x=x+3))
$ echo $x
8
$ ((x+=1))
$ echo $x
9
$ ((x++))
$ echo $x
10
$ ((x*=2))
$ echo $x
20
Conditionals in Bash
Insted of real conditionals in Bash we only have a way to some command was successful or not and the if will execute code based on this condition.
What does it mean a program is successful or not? Exit code 0 or anything else. $?
- [[ ]]}
- -e}
#!/usr/bin/env bash
filename=$1
if [[ -e $filename ]]; then
echo "File $filename exists"
fi
Bash exit code
#!/usr/bin/env bash
ls / > /dev/null 2> /dev/null
echo $? # 0
ls /qqrq > /dev/null 2> /dev/null
echo $? # 1
Bash exit code
#!/usr/bin/env bash
ls / > /dev/null 2> /dev/null
exit_code=$?
echo $exit_code
if [ $exit_code = 0 ]; then
echo /
fi
ls /qqrq > /dev/null 2> /dev/null
exit_code=$?
echo $exit_code
if [ $exit_code = 0 ]; then
echo /qqrq
fi
Make sure you save the exit code immediaely after the execution of some code if you'd like to use it later, as it always contains the exit code of the most recent statement.
Echo multiline string
#!/bin/bash
echo "
Line 1
Line 2
"
Redirect multiline string
#!/bin/bash
echo "
Line 1
Line 2
" > out
User Input (read)
#!/bin/bash
echo "Please type in your name"
read name
echo "Welcome '$name'"
Counter
COUNTER=0
echo $COUNTER
COUNTER=$((COUNTER+1))
echo $COUNTER
COUNTER=$((COUNTER+1))
echo $COUNTER
Declare
declare -a animals=(cow snail elephant mouse)
king=${animals[$(((RANDOM % 4)))]}
echo $king