$(command) 命令置换
is “command substitution”. As you seem to understand, it runs the command, captures its output, and inserts that into the command line that contains the $(…);
```
$ ls -ld $(date +%B).txt
-rwxr-xr-x 1 Noob Noob 867 Jul 2 11:09 July.txt
```
${parameter} 参数置换
is “parameter substitution”.
${parameter}
The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character which is not to be interpreted as part of its name.
${1:-default} 如果第一个参数为unset或 empty,就用default
"if parameter 1 is unset or empty, then use default instead".
s@rg:~/git/Temp$ set --
s@rg:~/git/Temp$ echo "${1:-2}"
2
s@rg:~/git/Temp$ set 234432 345534
s@rg:~/git/Temp$ echo "${1:-2}"
234432
s@rg:~/git/Temp$ echo "${1:-1}"
234432
s@rg:~/git/Temp$ echo "${0:-1}"
-bash
s@rg:~/git/Temp$ echo "${2:-1}"
345534
s@rg:~/git/Temp$ echo "${2: -1}"
4
s@rg:~/git/Temp$ echo "${2: -3}"
534
s@rg:~/git/Temp$ set --
s@rg:~/git/Temp$ echo "${1:-3}"
3
${1#*-} 从第一个参数中类似全局变量的删除 最短符合*-的数据串
deletes the shortest match of *-, a glob-like pattern from $1 variable.
# 如果第一个参数不包含 *—, 就
if [ "${1#*-}" = "$1" ]; then
echo "Do something"
fi
#test
foo="123-456-789~"
echo "${foo#*-}"
Output: 456-789~
$#, $@ & $?: Bash Built-in variables
$# 参数的个数 . Answer is 3
$@ 参数的具体信息 . Answer is 1 2 3
$? 最后一条指令的执行结果 . Answer is 0 which means ‘yes’
file:test.sh
#! /bin/sh
echo '$#' $#
echo '$@' $@
echo '$?' $?
> ./test.sh 1 2 3
You get output:
$# 3
$@ 1 2 3
$? 0
*You passed 3 parameters to your script.*