A shell is a program that acts as an interface between the user and the kernel, typically used in a terminal in text mode. Bash is the shell from the GNU project.
-
regular filed
directoryl
symbolic linkc
character deviceb
block devices
named socketp
named pipe$ pwd
$ cd /rep
$ ls
$ rm file
$ rmdir /rep
$ cp file1 file2
$ mv file1 file2
$ touch file
$ ln file link
$ cat file
$ find file
$ cut [-c]
$ tail [-n N]
$ head [-n N]
$ wc [-l -w -c]
$ sort [-u -n -r]
$ grep pattern [-v]
$ tr [set1] [set2] [-d -s]
$ tar [-x]
$ cmd1 | xargs cmd2
$ cmd1 | tee file.txt | cmd2
$ chmod 000 file
((expression))
→ true if expression is valid$((expression))
→ evaluates and substitutes result$ echo $((20+30))
50
$ echo "scale=8; 17/7" | bc
2.42857142
$ echo $((10 == 10)) # 1
$ echo $((10 >= 4 ? 1 : 0)) # 1
${var-val} # var if defined, else val
${var=val} # var if defined, else set to val
${var+val} # val if var is defined, else empty
${var:i} # substring from index i
${var:i:k} # substring from i, length k
${var:i:1} # character at i
${#var} # length of var
${var^^} # uppercase
${var,,} # lowercase
${var#pattern} # shortest match from start
${var##pattern} # longest match from start
${var%pattern} # shortest match from end
${var%%pattern} # longest match from end
$0 # script name
$1 # first argument
${10} # tenth argument
${!#} # last argument
$# # number of arguments
$* # all arguments
"$@" # all arguments (quoted)
$ cat << STOP
multiline text
STOP
$ cat >| file.txt << STOP
Text block
$(command block)
More text
STOP
. /path/to/file
#!/bin/bash
if cmd ; then block ; fi
if cmd ; then
block
elif cmd2 ; then
block2
else
fallback
fi
case word in
val1) block1 ;;
val2) block2 ;;
*) default_block ;;
esac
while cmd ; do block ; done
while cmd ; do
block
done
for var in list ; do block ; done
for var in list ; do
block
done
my_function () {
# function body
}
local var=value
t[0]=element
t=(one two three)
echo "${t[1]}" # second element
echo "${t[*]}" # all elements
${#t[*]}
test -e file # exists
test -f file # regular file
test -d file # directory
test -L file # symbolic link
test -r file # readable
test -w file # writable
test -x file # executable
test -N file # modified since last read
test file1 -nt file2 # newer than
test x -eq y # equal
test x -ne y # not equal
test x -lt y # less than
test x -le y # less or equal
test x -gt y # greater than
test x -ge y # greater or equal
test -z str # empty
test -n str # not empty
test str1 = str2 # equal
test str1 != str2 # not equal
test str1 \< str2 # lexicographically before
test str1 \> str2 # lexicographically after
test -v var # variable exists
test -o opt # shell option set
test ! expr # logical NOT
test a -a b # logical AND
test a -o b # logical OR
test \( expr \) # parentheses
echo "${file##*/}"
echo "${file%%.*}"
lister_extensions() {
local base="$1"
local ext0=${base##*.}
for file in "$base"*; do
local ext=${file#$base}
if [[ "$ext" = "$ext0" ]]; then
echo "''"
else
echo "${ext#.}"
fi
done
}
est_entier() {
local number=$1
expr "$number" + 1 > /dev/null 2>&1
}