2026/4/6 4:17:37
网站建设
项目流程
apache设置网站网址,优化排名案例,企业自建微博的特点,安徽建设工程信息管理平台以下是在Shell脚本中常见的字符串操作功能总结#xff0c;涵盖了各种处理字符串的场景#xff1a;1. 基本定义和赋值strHello World # 双引号#xff08;允许变量扩展#xff09;
strHello World # 单引号#xff08;原样输出#xff09;
…以下是在Shell脚本中常见的字符串操作功能总结涵盖了各种处理字符串的场景1.基本定义和赋值strHello World # 双引号允许变量扩展 strHello World # 单引号原样输出 str$Hello\nWorld # ANSI-C引用支持转义字符2.字符串长度${#string} # 获取字符串长度 echo ${#str} # 输出113.子字符串提取${string:position} # 从指定位置开始提取 ${string:position:length} # 从指定位置提取指定长度 strHello World echo ${str:6} # 输出World echo ${str:6:3} # 输出Wor echo ${str: -5} # 输出World负数从末尾开始4.字符串删除从开头删除${string#substring} # 删除最短匹配的前缀 ${string##substring} # 删除最长匹配的前缀 path/usr/local/bin/app echo ${path#/*/} # 输出local/bin/app echo ${path##/*/} # 输出app5.字符串删除从末尾删除${string%substring} # 删除最短匹配的后缀 ${string%%substring} # 删除最长匹配的后缀 fileapp.tar.gz echo ${file%.*} # 输出app.tar echo ${file%%.*} # 输出app echo ${file%.gz} # 输出app.tar6.字符串替换${string/pattern/replacement} # 替换第一个匹配 ${string//pattern/replacement} # 替换所有匹配 ${string/#pattern/replacement} # 替换开头的匹配 ${string/%pattern/replacement} # 替换结尾的匹配 strhello world hello echo ${str/hello/HI} # 输出HI world hello echo ${str//hello/HI} # 输出HI world HI echo ${str/#hello/HI} # 输出HI world hello7.大小写转换Bash 4.0${string^^} # 转大写 ${string,,} # 转小写 ${string^} # 首字母大写 ${string,} # 首字母小写 strhello World echo ${str^^} # 输出HELLO WORLD echo ${str,,} # 输出hello world echo ${str^} # 输出Hello World8.字符串查找位置# 查找子字符串位置 expr index $string $substring # 返回字符位置 expr match $string $pattern # 返回匹配长度 expr substr $string $position $length # 子串截取 strhello world expr index $str wo # 输出7 expr match $str hello # 输出5 expr substr $str 2 3 # 输出ell9.字符串比较[[ $str1 $str2 ]] # 相等比较 [[ $str1 ! $str2 ]] # 不等比较 [[ $str1 $str2 ]] # 按字典序小于 [[ $str1 $str2 ]] # 按字典序大于 [[ -z $str ]] # 是否为空 [[ -n $str ]] # 是否非空 [[ $str ~ regex ]] # 正则匹配 if [[ hello ~ ^h.*o$ ]]; then echo 匹配 fi10.字符串分割# 使用IFS分割为数组 IFS, read -ra array a,b,c,d echo ${array[1]} # 输出b # 使用参数展开 strone:two:three arr(${str//:/ }) # 将冒号替换为空格再转换为数组11.字符串拼接str1Hello str2World result$str1 $str2 # 输出Hello World result$str1$str2 # 输出HelloWorld result${str1}_${str2} # 输出Hello_World12.去除空白字符# 去除开头空白 ${string#${string%%[![:space:]]*}} # 去除结尾空白 ${string%${string##*[![:space:]]}} # 使用sed echo $str | sed -e s/^[[:space:]]*// -e s/[[:space:]]*$//13.字符串包含判断# 方法1使用通配符 if [[ $str *substring* ]]; then echo 包含 fi # 方法2使用正则 if [[ $str ~ substring ]]; then echo 包含 fi # 方法3使用grep if echo $str | grep -q substring; then echo 包含 fi14.字符串反转strhello rev$(echo $str | rev) # 需要rev命令支持 echo $rev # 输出olleh ######################################### # 纯Bash实现 reverse() { local str$1 local rev for ((i${#str}-1; i0; i--)); do rev$rev${str:$i:1} done echo $rev }15.字符串统计# 统计字符出现次数 strhello world charl count$(echo ${str} | awk -F${char} {print NF-1}) echo $count # 输出3 # 统计单词数 echo $str | wc -w16.进制转换# 十进制转十六进制 printf %x\n 255 # 输出ff # 十六进制转十进制 echo $((0xff)) # 输出255 # 二进制转十进制 echo $((2#11111111)) # 输出25517.使用外部命令处理字符串# 使用awk echo hello:world | awk -F: {print $2} # 使用sed echo hello world | sed s/hello/HI/ # 使用cut echo hello:world | cut -d: -f2 # 使用tr字符转换 echo HELLO | tr A-Z a-z # 转小写 echo hello | tr a-z A-Z # 转大写 echo hello | tr -d aeiou # 删除元音字母18.特殊字符处理# URL编码/解码 urlencode() { local string${1} local strlen${#string} local encoded for (( pos0 ; posstrlen ; pos )); do c${string:$pos:1} case $c in [-_.~a-zA-Z0-9] ) o${c} ;; * ) printf -v o %%%02x $c esac encoded${o} done echo ${encoded} } # Base64编码/解码 echo hello | base64 # 编码 echo aGVsbG8 | base64 -d # 解码19.字符串填充和对齐# 使用printf格式化 printf %-10s hello # 左对齐宽度10 printf %10s hello # 右对齐宽度10 printf %010d 123 # 用0填充0000000123 # 在字符串前后添加固定字符 printf %0.s- {1..10} # 生成10个连字符20.多行字符串处理# Here Document cat EOF 第一行 第二行 第三行 EOF # 删除多行字符串中的空行 echo -e line1\n\nline2\n\nline3 | sed /^$/d实用示例组合#!/bin/bash # 综合示例处理文件名 filenamemyfile.tar.gz # 获取不同部分 basename${filename%.*} # myfile.tar extension${filename##*.} # gz name${basename%.*} # myfile full_extension${filename#*.} # tar.gz echo 文件名: $filename echo 基本名: $basename echo 扩展名: $extension echo 名称: $name echo 完整扩展名: $full_extension