在bash中使用$ {1:1} [英] Using ${1:1} in bash

查看:459
本文介绍了在bash中使用$ {1:1}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道${1:1}在bash中意味着什么吗?

does someone knows what ${1:1} means in bash?

例如:

for (( i=0; $i < ${1:1}; i++ ))
do
    addToList $2
done

推荐答案

那是一场狂欢特别是:

${parameter:offset}
${parameter:offset:length}

这称为子字符串扩展.它可以扩展到参数值的最大长度字符,从offset指定的字符开始.如果参数为"@",下标为"@"或"*"的索引数组或关联数组名称,则结果将有所不同,如下所述.如果省略length,它将扩展到parameter值的子字符串,该字符串从offset指定的字符开始,并扩展到value的末尾.长度和偏移量是算术表达式(请参见Shell Arithmetic).

This is referred to as Substring Expansion. It expands to up to length characters of the value of parameter starting at the character specified by offset. If parameter is ‘@’, an indexed array subscripted by ‘@’ or ‘*’, or an associative array name, the results differ as described below. If length is omitted, it expands to the substring of the value of parameter starting at the character specified by offset and extending to the end of the value. length and offset are arithmetic expressions (see Shell Arithmetic).

以下是一些说明参数和下标数组的子字符串扩展的示例:

Here are some examples illustrating substring expansion on parameters and subscripted arrays:

$ string=01234567890abcdefgh
$ echo ${string:7}
7890abcdefgh

因此${1:1}从第二个字符(1个未指定的lengthoffset)开始扩展位置参数1的子字符串.

So ${1:1} is expand the substring of positional parameter 1 starting from the second character (offset of 1 unspecified length).

大概是为了支持带有以下参数的脚本:

Presumably that is intended to support a script taking arguments something like this:

./addn -10 A

将十个A元素添加到addToList要添加的内容中.

to add ten A elements to whatever addToList is adding to.

但这是一种糟糕的处理方式,除非在此之前对参数进行了有效性/理智性检查.

Though that's a poor way of handling that unless the argument has been checked before that for validity/sanity.

这篇关于在bash中使用$ {1:1}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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