Bash Basic
2018-11-27
Tech
730
Substitution, dirname, basename and suffix
Substitution can be used to get path and short filename.
filename=a/b/c/name.file
echo ${filename#*/} # b/c/name.file
echo ${filename##*/} # name.file
echo ${filename%/*} # a/b/c
echo ${filename%%/*} # a
But in fact, there is better way:
filename=a/b/c/name.file
echo $(dirname $filename)
echo $(basename $filename)
Still, substitution is useful for getting filename without suffix or getting suffix.
filename=file.name.type
echo ${filename%.*} # file.name
echo ${filename##*.} # type
Parameter Expansion
String selection:
string=1234567890abcdefg
echo ${string: 10} # abcdefg
echo ${string: 10:5} # abcde
echo ${string: 7:-4} # 890abc
Array selection:
arr=(0 1 2)
arr=($