鱼C论坛

 找回密码
 立即注册
查看: 638|回复: 4

[已解决]bash $temp

[复制链接]
发表于 2023-9-23 21:38:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Given a variable $temp, what is the effect of ${#temp} , ${temp%%word}, and ${temp/pattern/string}  ?
最佳答案
2023-9-23 21:39:10
In Bash scripting, the expressions ${#temp}, ${temp%%word}, and ${temp/pattern/string} allow you to manipulate the value of the variable $temp in different ways.

1. ${#temp}: This expression returns the length of the value stored in the variable $temp. For example, if $temp contains the string "Hello", ${#temp} will evaluate to 5.

2. ${temp%%word}: This expression removes the longest matching suffix pattern "word" from the value of $temp. It returns the resulting string after removing the suffix. For example, if $temp contains the string "/path/to/file.txt", ${temp%%/*} will remove "/path" and return "to/file.txt". This is known as "parameter expansion".

3. ${temp/pattern/string}: This expression performs pattern substitution within the value of $temp. It replaces the first occurrence of the pattern with the given string. For example, if $temp contains the string "Hello, world!", ${temp/o/0} will replace the first 'o' character with '0' and return "Hell0, world!". If you want to replace all occurrences of the pattern, you can use ${temp//o/0}. Again, this is a form of "parameter expansion".

These expressions are powerful tools in Bash for manipulating string variables and performing operations like length calculation, pattern matching, and substitution.
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-23 21:39:05 | 显示全部楼层
1. ${#temp}:返回变量 $temp 的长度。
2. ${temp%%word}:从变量 $temp 的末尾开始,删除最长匹配的字符串 "word",并返回剩余的字符串。
3. ${temp/pattern/string}:将变量 $temp 中第一个匹配 "pattern" 的字符串替换为 "string",并返回替换后的字符串。如果使用 /g 选项,则会替换所有匹配的字符串。

举个例子:

假设 $temp 的值为 "hello world"。

1. ${#temp} 的结果为 11,因为 "hello world" 这个字符串的长度为 11。
2. ${temp%%world} 的结果为 "hello ",因为从变量 $temp 的末尾开始,最长匹配的字符串是 "world",所以删除了 "world",并返回剩余的字符串 "hello "。
3. ${temp/o/a} 的结果为 "hello warld",因为它将第一个匹配 "o" 的字符替换为 "a",并返回替换后的字符串 "hello warld"。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-23 21:39:10 | 显示全部楼层    本楼为最佳答案   
In Bash scripting, the expressions ${#temp}, ${temp%%word}, and ${temp/pattern/string} allow you to manipulate the value of the variable $temp in different ways.

1. ${#temp}: This expression returns the length of the value stored in the variable $temp. For example, if $temp contains the string "Hello", ${#temp} will evaluate to 5.

2. ${temp%%word}: This expression removes the longest matching suffix pattern "word" from the value of $temp. It returns the resulting string after removing the suffix. For example, if $temp contains the string "/path/to/file.txt", ${temp%%/*} will remove "/path" and return "to/file.txt". This is known as "parameter expansion".

3. ${temp/pattern/string}: This expression performs pattern substitution within the value of $temp. It replaces the first occurrence of the pattern with the given string. For example, if $temp contains the string "Hello, world!", ${temp/o/0} will replace the first 'o' character with '0' and return "Hell0, world!". If you want to replace all occurrences of the pattern, you can use ${temp//o/0}. Again, this is a form of "parameter expansion".

These expressions are powerful tools in Bash for manipulating string variables and performing operations like length calculation, pattern matching, and substitution.
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-23 21:39:16 | 显示全部楼层
${#temp}: 这个表达式获取了变量 $temp 的长度,即字符串的字符个数。例如,如果 $temp 的值为 "Hello",那么 ${#temp} 将会返回 5。

${temp%%word}: 这个表达式从变量 $temp 的末尾开始匹配并删除最长的匹配项 "word"。其中 %% 表示删除后缀。例如,如果 $temp 的值为 "HelloWorld",而 "World" 是要删除的子字符串,那么 ${temp%%World} 将会返回 "Hello"。

${temp/pattern/string}: 这个表达式将变量 $temp 中第一个匹配 pattern 的子字符串替换为 string。其中 / 是替换操作符。如果 pattern 匹配多个子字符串,则只替换第一个匹配项。例如,如果 $temp 的值为 "HelloWorld",而我们想把 "World" 替换为 "Universe",那么 ${temp/World/Universe} 将会返回 "HelloUniverse"。

需要注意的是,这些表达式都是针对字符串操作的,可以用于处理变量中存储的文本内容。另外,这些表达式支持使用通配符进行模式匹配,可以更灵活地应用于字符串处理。

球一个最佳答案谢谢啦!这对我非常重要!
回答可能有误,多多谅解!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-23 21:43:58 | 显示全部楼层
陶远航 发表于 2023-9-23 18:39
In Bash scripting, the expressions ${#temp}, ${temp%%word}, and ${temp/pattern/string} allow you to  ...

我嘞个豆,英格雷是
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-21 23:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表