#!/bin/bash
for i in {1..10}
# Simpler and more straightforward than
#+ for i in $(seq 10)
do
echo -n "$i "
done
echo
# 1 2 3 4 5 6 7 8 9 10
# Or just . . .
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
echo {z..a} # z y x w v u t s r q p o n m l k j i h g f e d c b a
# Works backwards, too.
echo {3..-2} # 3 2 1 0 -1 -2
echo {X..d} # X Y Z [ ] ^ _ ` a b c d
# Shows (some of) the the ASCII characters between Z and a,
#+ but don't rely on this type of behavior because . . .
echo {]..a} # {]..a}
# Why?
The ${!array[@]} operator, which
expands to all the indices of a given array.
#!/bin/bash
Array=(element-zero element-one element-two element-three)
echo ${Array[0]} # element-zero
# First element of array.
echo ${!Array[@]} # 0 1 2 3
# All the indices of Array.
for i in ${!Array[@]}
do
echo ${Array[i]} # element-zero
# element-one
# element-two
# element-three
#
# All the elements in Array.
done
#!/bin/bash
variable="This is a fine mess."
echo "$variable"
if [[ "$variable" =~ "T*fin*es*" ]]
# Regex matching with =~ operator within [[ double brackets ]].
then
echo "match found"
# match found
fi
Or, more usefully:
#!/bin/bash
input=$1
if [[ "$input" =~ "[1-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
# NNN-NN-NNNN
# Where each N is a digit.
# But, initial digit must not be 0.
then
echo "Social Security number."
# Process SSN.
else
echo "Not a Social Security number!"
# Or, ask for corrected input.
fi
The new set -o pipefail option is
useful for debugging pipes. If
this option is set, then the exit status of a pipe
is the exit status of the last command in the pipe to
fail (return a non-zero value), rather
than the actual final command in the pipe.