0

I found code for getting the current Caps Lock status (since this laptop's keyboard doesn't have any LED indicators for it) from here.

#!/bin/bash
v=`xset -q | grep Caps`

echo ${v:7:17}

I understand how the first part works; it's querying for the status then looking for the string "Caps" and storing that to a variable. What I don't understand is this line here:

echo ${v:7:17}

That line just prints out "Caps Lock: off/on", depending on the Caps Lock status. I imagine the numbers and colons are for specifying a range so extraneous info doesn't get printed, but the numbers don't seem to correspond to the characters being printed out in any way I can see. The full line that would be printed out is something like:

    00: Caps Lock:   off    01: Num Lock:    on     02: Scroll Lock: off

What I'm asking is: what exactly is the range specifying? Is it saying v:start:end, essentially? I've tried looking up info on using ranges with echo, but didn't find anything. My system's manpages don't even mention this usage for echo.

  • Did you try playing with the number to see what effect they have on the output produced? – Andy Dalton Feb 13 '17 at 16:47
  • 2
    ${v:7:17} is a substring expansion performed by the shell, before echo gets involved. As for the specific numbers - remember that spaces are characters too. – steeldriver Feb 13 '17 at 16:47
  • @AndyDalton: I did try experimenting with different ranges, and I can see that it changed which portion got printed out, but I couldn't identify any logical pattern behind it. – alyms108 Feb 13 '17 at 18:07
1

See Substring Expansion. The format is ${string:position:length}. Consider, for example:

$ x="123456789012345678901234567890"
$ echo ${x:0:0}
$ echo ${x:0:1}
1
$ echo ${x:0:2}
$ echo ${x:0:3}
123
$ echo ${x:1:3}
234
  • I think I'm understanding a bit better now. Unfortunately, there's a discrepancy between the output of the xset|grep command itself and the output of echoing the contents as stored in the variable. When I echo the contents of the variable, the extra whitespace gets stripped, so it's hard to tell if the offset and length are based on the command output or on the contents of the variable. For example, the leading whitespace is one of the things that gets stripped when the output is stored to the variable. – alyms108 Feb 13 '17 at 18:35
  • You haven't shown any code, so it's hard to speculate on what may or may not be getting written to the variable. You can do echo "'${v}'" to print the value of v surrounded by single quotes -- that's the value to which the range operation would apply. – Andy Dalton Feb 13 '17 at 18:38
  • Messing around with echo "'${v}'" helped a lot. Now I can see how offset` and length are producing the output they do. What threw me off was when I echo ${v:7:17} with no quotes, extra whitespace gets stripped off, which of course changes the length of the string. Thank you. – alyms108 Feb 13 '17 at 18:49
1

It's not about echo. It's about the shell. If you search through man bash using this command, for example,

man bash | grep -C5 {

you will see a description like this

${parameter:offset}
${parameter: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 spec‐
       ified by offset and extending to the end of the value.  length and offset are arithmetic expressions (see ARITHMETIC EVAL‐
       UATION below).

echo only prints the substring, but printf would do the same.

The whole description in man bash:

${parameter:offset}
${parameter: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 asso‐
       ciative array name, the results differ as described below.  If length
       is omitted, expands to the substring of the value of parameter start‐
       ing at the character specified by offset and extending to the end  of
       the  value.  length and offset are arithmetic expressions (see ARITH‐
       METIC 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  con‐
       fused with the :- expansion.

       If  parameter is @, the result is length positional parameters begin‐
       ning 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  ${parame‐
       ter[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 unde‐
       fined 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.