help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] print float number


From: Bob Proulx
Subject: Re: [Help-bash] print float number
Date: Thu, 9 Jan 2014 12:09:34 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Bob Proulx wrote:
> I am sure there is a better way to grab the last two characters from a
> string.  Suggestions?  I thought only of this monstrosity.
> 
>   foo=12345
>   echo ${#foo}
>   5
>   echo ${foo:$((${#foo} - 2))}
>
> Making it quite an ugly and unobvious collection of modem line noise.

Ah!  I hadn't realized that offset and lengths could be negative.
(With the required space ": -"so that it isn't confused with ":-".)

  foo=12345
  echo ${foo: -2}
  45
  echo ${foo:0: -2}
  123

That cleans this up significantly.

  for ((i=100; i<=110; i++)); do
    dollars=${i:0: -2}
    cents=${i: -2}
    echo $dollars.$cents
  done

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]