Linux Bash 小结

$(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.*
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值