shell parameter expansitions

type test
type -a test

math calculate:
echo $((1+2*3))

parameter expansition:
bash-4 introduced features:
var=student
echo ${var^}  //Student
echo ${var^^} //STUDENT
var=STUDENT
echo ${var,}  //sTUDENT
echo ${var,,} //student
bash2 introduced features:
${var//PATTERN/STRING}:replace all instances of pattern with string
passwd=12345678
printf "%s " "${passwd//?/*}" //********
${var:OFFSET:LENGTH}:return a substring of $var
var=student
echo ${var:0:3} //stu
POSIX SHELL
posix shell introduced some expansions from kornshell.
main returning the length and removing a pattern from the beginning
or end of a variable's contents.
${#var}:length of variable's contents
var=student
echo "${#var}" //7
${var%PATTERN}:remove the shortest match from the end
var=tomorrow
echo ${var%o*} //tomorr
${var%%PATTERN}:remove the longgest match from the end
var=tomorrow
echo ${var%%o*} //t
${var#PATTERN}:remove the shortest match from the beginning
var=tomorrow
echo ${var#*o} //morrow
${var##PATTERN}:Remove the longest match from the beginning
echo ${var##*o} //w

${var:-default} and ${var-default}: use default values
${var:-default}
if the var is unset or empty, then use default
var=
echo ${var:-default} //default
unset var
echo ${var:-default} //default
${var-default}
if the var is unset , then use default , just this case
unset var
echo ${var-default} //default
var=
echo ${var-default} //



原文地址:https://www.cnblogs.com/huaxiaoyao/p/5995826.html