Bash-'x = $ {1:3:1}'是什么意思? [英] Bash - what does 'x=${1:3:1}' mean?

查看:293
本文介绍了Bash-'x = $ {1:3:1}'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本的功能中,我看到了类似的东西

Inside the function of a shell script I see something like this

func()
{
 local x
 x=${1:3:1}
...
}

x=${1:3:1}是什么意思?我知道$1$2$3是该函数的参数.那上面的陈述是否意味着x = $1:$2:$3?

What does x=${1:3:1} mean? I know that $1, $2 and $3 are arguments of the function. So does the above statement mean that x = $1:$2:$3?

此外,如果有人可以建议我如何在Google上搜索像这样的特殊字符,这真的很有帮助?有标准关键字吗?我尝试搜索"shell脚本中的:"是什么"等.但是当尝试搜索特殊字符时,结果是随机的.

Also, it is really helpful if someone can suggest on how do I google search for speacial characters like this? Any standard keywords? I tried searching 'what is ":" in shell scripts' etc.. But the results are random when trying to search for special characters.

推荐答案

这在shell中称为参数扩展.

This is called parameter expansion in shell.

$ {PARAMETER:OFFSET:LENGTH}

${PARAMETER:OFFSET:LENGTH}

给定开始位置和长度,此参数只能扩展参数值的一部分.如果省略LENGTH,则参数将扩展到字符串的末尾.如果LENGTH为负,则从字符串的末尾算起,作为字符串的第二个偏移量.

This one can expand only a part of a parameter's value, given a position to start and maybe a length. If LENGTH is omitted, the parameter will be expanded up to the end of the string. If LENGTH is negative, it's taken as a second offset into the string, counting from the end of the string.

OFFSET和LENGTH可以是任何算术表达式.偏移量从0开始,而不是1.

OFFSET and LENGTH can be any arithmetic expression. The OFFSET starts at 0, not at 1.

例如,假设参数是字符串,

e.g lets say the parameter is a string,

MYSTRING =在接受的内容上保持自由,在发送的内容上保持保守"

MYSTRING = "Be liberal in what you accept, and conservative in what you send"

回显$ {MYSTRING:34:13}

echo ${MYSTRING:34:13}

以上内容将为您提供以下内容

The above will give you the following

保守

,因为它将计算第33个字符(索引从0开始),该字符将以字符"c"开头.然后计算(13个字符)长度.

as it will count the 33th(index start at 0) character which will start with the character "c" and then count (13 charcter) length .

因此,在您的情况下,$ 1是您传递给脚本的参数,然后它偏移该脚本的3个字符并采用长度为1的字符串并将其初始化为x.

在此处了解更多信息: http://wiki.bash-hackers.org/syntax /pe#substring_expansion

Read more here : http://wiki.bash-hackers.org/syntax/pe#substring_expansion

这篇关于Bash-'x = $ {1:3:1}'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆