bash学习笔记3-字符串操作

本文详细介绍了Bash中各种字符串操作符的用法,包括替换、匹配和子串提取等,帮助读者掌握如何高效地处理字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 字符串操作说明(摘自《Learning The Bash Shell》)

替换操作:

Operator

Substitution

${varname:-word}

If varname exists and isn't null, return its value; otherwise return word.

Purpose:

Returning a default value if the variable is undefined.

Example:

${count:-0} evaluates to 0 if count is undefined.

${varname:=word}

If varname exists and isn't null, return its value; otherwise set it to word and then return its value. Positional and special parameters cannot be assigned this way.

Purpose:

Setting a variable to a default value if it is undefined.

Example:

${count:=0} sets count to 0 if it is undefined.

${varname:?message}

If varname exists and isn't null, return its value; otherwise print varname:followed by message, and abort the current command or script (non-interactive shells only). Omitting message produces the default message parameter null or not set.

Purpose:

Catching errors that result from variables being undefined.

Example:

{count:?"undefined!"} prints "count: undefined!" and exits if count is undefined.

${varname:+word}

If varname exists and isn't null, return word; otherwise return null.

Purpose:

Testing for the existence of a variable.

Example:

${count:+1} returns 1 (which could mean "true") if count is defined.

${varname:offset}

 

${varname:offset:length}

Performs substring expansion.a It returns the substring of $varname starting at offset and up to length characters. The first character in $varname is position 0. If length is omitted, the substring starts at offset and continues to the end of $varname. If offset is less than 0 then the position is taken from the end of $varname. If varname is @, the length is the number of positional parameters starting at parameter offset.

Purpose:

Returning parts of a string (substrings or slices).

Example:

If count is set to frogfootman, ${count:4} returns footman. ${count:4:4} returns foot.

 

字符串匹配:

(pattern使用*,?,[ 和 ]字符)

Operator
Meaning
${variable#pattern}
If the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest.
${variable##pattern}
If the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest.
${variable%pattern}
If the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest.
${variable%%pattern}
If the pattern matches the end of the variable's value, delete the longest part that matches and return the rest.
${variable/pattern/string}
 
${variable//pattern/string}
The longest match to pattern in variable is replaced by string. In the first form, only the first match is replaced. In the second form, all matches are replaced. If the pattern is begins with a #, it must match at the start of the variable. If it begins with a %, it must match with the end of the variable. If string is null, the matches are deleted. If variable is @ or *, the operation is applied to each positional parameter in turn and the expansion is the resultant list.a
例:
Expression           Result
${path##/*/}                      long.file.name
${path#/*/}              cam/book/long.file.name
$path              /home/cam/book/long.file.name
${path%.*}         /home/cam/book/long.file
${path%%.*}        /home/cam/book/long

 ${#varname}返回字符串长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值