In Linux shell scripts what does 'x=${1:3:1}' mean?

匆匆过客 提交于 2019-12-24 08:16:11

问题


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

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

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?

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.


回答1:


This is called parameter expansion in shell.

${PARAMETER:OFFSET: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 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 = "Be liberal in what you accept, and conservative in what you send"

echo ${MYSTRING:34:13}

The above will give you the following

conservative

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

So in your case $1 is the parameter you pass to your script and then it offsets 3 characters of that and take a string of length 1 and initialize it to x.

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




回答2:


It is a parameter expansion, part of many that start with ${.

Like ${parameter:-word}, ${parameter:=word}, ${parameter:?word}, ${parameter:+word} and several others.

This one (specific to ksh, bash and zsh): ${parameter:offset:length} extracts lenght characters (optional, if missing, the rest of the string in parameter) starting at offset. With several details described in the bash manual.

${name:offset:length}
  Substring Expansion.  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,  
  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 ARITHMETIC EVALUATION below).

  If  offset evaluates to a number less than zero, the value is used as an  
  offset in characters from the end of the value of parameter.  If length  
  evaluates to a number less than zero, it is interpreted as an offset in  
  characters from the end of the value of parameter rather than a number  
  of characters, and the expansion is the characters between offset and  
  that result.  Note that a negative offset must be separated from the  
  colon by at least one space to avoid being confused with the :- expansion.

  If parameter is @, the result is length positional parameters  
  beginning at offset.  A negative offset is taken relative to one greater  
  than the greatest positional parameter, so an offset of -1 evaluates  
  to the last positional parameter.  It is an expansion error if length  
  evaluates to a number less than zero.

  If parameter is an indexed array name subscripted by @ or *, the result  
    is the length members of the array beginning with ${parameter[offset]}.  
    A  negative offset is taken relative to one greater than the maximum  
    index of the specified array.  It is an expansion error if length evaluates  
    to a number less than zero.

  Substring expansion applied to an associative array produces undefined  
    results.  

  Substring indexing is zero-based unless the positional parameters  
    are used, in which case the indexing starts at 1 by default. If  
    offset is  0, and the positional parameters are used, $0 is prefixed  
    to the list.



回答3:


Use the manual page, all information is in there. man bash:

   ${parameter:offset:length}
          Substring  Expansion.  Expands to up to length characters of the
          value of parameter starting at the character specified  by  off‐
          set.  If parameter is @, an indexed array subscripted by @ or *,
          or an associative array name, the results  differ  as  described
          below.   If  length  is omitted, 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 ARITHMETIC EVALUATION below).



回答4:


What does x=${1:3:1} mean?

It's substring cut, and in English: using the string in $1, pull 1 character starting at index 3 (where indexes are 0-based). So if $1 === "foobar", then ${1:3:1} === "b".

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

No, adjacency represents string concatenation: x="$1$2$3" is the result of concatenating the strings in $1, $2, and $3.

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.

bash parameter substitution usually gets you in the ballpark. I know I can't remember all the different syntax ways bash can fiddle with the data, so committing "parameter substitution" to memory pays off. String manipulation happens to be the chapter before parameter substitution.




回答5:


Try this:

set ABCDEFG

echo ${1:3:1}

It is getting a substring. In general ${} refers to an array variable (in this case array of chars)



来源:https://stackoverflow.com/questions/40622680/in-linux-shell-scripts-what-does-x-131-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!