to file
stderr to stdout
stderr and stdout 2 file
stdout to stderr
ls -l > ls-l.txt
ls not_exist_file 2> &1
grep for 1>&2
rm -f $(find / -name core_hello) &> /dev/null
ls -l | sed -e "s/[aeio]/u/g"
ls -l | grep "\.txt$"
if expression then statement1 else statement2
if expression1 then statement1 else if expression2 then statement2 else statement3
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
STR="Hello World!"
echo $STR
OF=/var/my-backup-$(date +%Y%m%d).tgz
tar -zcvf $OF /home/me/
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
for i in {1..5}
do
echo "Welcome $i times"
done
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
do
echo "Welcome $i times"
done
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times"
done
for I in 1 2 3 4 5
do
statements1
statements2
if (disaster-condition)
then
break #Abandon the loop.
fi
statements3 #While good and, no disaster-condition.
done
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
function hello {
echo Hello!
}
hello
function e {
echo $1
}
e Hello
-lt (<)
-gt (>)
-le (<=)
-ge (>=)
-eq (==)
-ne (!=)
s1 = s2
s1 != s2
s1 < s2
s1 > s2
for i in {1..100..1};
do let j=$i%7 ;
if [ $j -eq 0 ] ;
then echo $i;
fi;
done;
wc -w -l -c vimtest.txt
grep "hello" vimtest.txt -c
sed 's/to_be_replaced/replaced/g' vimtest.txt
sed '2,5d' vimtest.txt
#!/bin/bash
OPTIONS="Hello Quit"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo done
exit
elif [ "$opt" = "Hello" ]; then
echo Hello World
else
clear
echo bad option
fi
done
Test if the program has received an argument
#!/bin/bash
if [ -z "$1" ]; then
echo usage: $0 directory
exit
fi
read NAME
echo "Hi $NAME!"
echo $((1+1))
echo $[1+1]
$?
#!/bin/bash
set -ex