Shell 字符串操作速查

作者: AlienPaul | 来源:发表于2022-04-22 10:20 被阅读0次

默认值设定

这块不好记住,直接看表格查找想要的就行。

+--------------------+----------------------+-----------------+-----------------+
|   Expression       |       parameter      |     parameter   |    parameter    |
|   in script:       |   Set and Not Null   |   Set But Null  |      Unset      |
+--------------------+----------------------+-----------------+-----------------+
| ${parameter:-word} | substitute parameter | substitute word | substitute word |
| ${parameter-word}  | substitute parameter | substitute null | substitute word |
| ${parameter:=word} | substitute parameter | assign word     | assign word     |
| ${parameter=word}  | substitute parameter | substitute null | assign word     |
| ${parameter:?word} | substitute parameter | error, exit     | error, exit     |
| ${parameter?word}  | substitute parameter | substitute null | error, exit     |
| ${parameter:+word} | substitute word      | substitute null | substitute null |
| ${parameter+word}  | substitute word      | substitute word | substitute null |
+--------------------+----------------------+-----------------+-----------------+
+--------------------+----------------------+-----------------+-----------------+
|   Expression       |  When FOO="world"    |  When FOO=""    |    unset FOO    |
|   in script:       |  (Set and Not Null)  |  (Set But Null) |     (Unset)     |
+--------------------+----------------------+-----------------+-----------------+
| ${FOO:-hello}      | world                | hello           | hello           |
| ${FOO-hello}       | world                | ""              | hello           |
| ${FOO:=hello}      | world                | FOO=hello       | FOO=hello       |
| ${FOO=hello}       | world                | ""              | FOO=hello       |
| ${FOO:?hello}      | world                | error, exit     | error, exit     |
| ${FOO?hello}       | world                | ""              | error, exit     |
| ${FOO:+hello}      | hello                | ""              | ""              |
| ${FOO+hello}       | hello                | hello           | ""              |
+--------------------+----------------------+-----------------+-----------------+

表格出处链接:https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash

规律:

  • "-"表示变量未赋值的时候替换
  • "+"表示变量已赋值的时候替换
  • "="表示未赋值的时候赋值
  • ":"表示变量已赋值但是为空字符串的时候也替换/受影响
  • "?"表示未赋值的时候报错,"?"后面的字符串为报错信息

获取长度

${#variableName}

从左侧删除

${var#Pattern} # 从左侧开始删除到第一个pattern匹配(pattern匹配也会删除)
${var##Pattern} # 从左侧开始删除到最后一个pattern匹配(pattern匹配也会删除)

从右侧删除

${var%pattern} # 从右侧开始删除到第一个pattern匹配(pattern匹配也会删除)
${var%%pattern} # 从右侧开始删除到最后一个pattern匹配(pattern匹配也会删除)

字符串内查找替换

${varName/Pattern/Replacement} # 替换从左第一个遇到的pattern
${varName//Pattern/Replacement} # 替换所有的pattern
# 例如
${varName/word1/word2}
${os/Unix/Linux}

字符串index范围提取

${variable:index} # 从字符串index开始提取到末尾
${variable:index:length} # 从字符串index开始提取length个字符

获取名字匹配的变量名

VECH="Bus"
VECH1="Car"
VECH2="Train"
echo "${!VECH*}" # 获取所有VECH开头的变量名,返回数组类型

大小写转换

${varName^} # 首字母大写
${varName^^} # 全部大写
${varName,} # 首字母小写
${varName,,} # 全部小写

参考链接

https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html

相关文章

网友评论

    本文标题:Shell 字符串操作速查

    本文链接:https://www.haomeiwen.com/subject/omggertx.html