4
p=$( power )
echo "Power now: ${p}W"

prints

Wower now: 357

This makes no sense. Surely this is a bug?

1
  • 1
    please add the output from uname -srv;type power;echo $SHELL $BASH_VERSION (My WSL2 installation doesn't have a power cmd). It's probably that a \r char has been introduced.
    – shellter
    Dec 14, 2023 at 20:06

1 Answer 1

8
Wower now: 357

Ah yes, what this actually is is the following sequence, one byte after the other

P (0x50)
o (0x6f)
w (0x77)
e (0x65)
r (0x72)
  (0x20)
n (0x6e)
o (0x6f)
w (0x77)
: (0x3a)
  (0x20)
3 (0x33)
5 (0x35)
7 (0x37)
jump-back-to-beginning-of-line (0x0d) ## called "carriage return", if you know typewriters…
W (0x57)

So, your power command, for reasons unbeknownst to humankind, outputs 357\r (carriage return).

Solution: only print things that are actually numbers coming from power, for example:

p=$( power | grep -o '[[:digit:]]*' )
echo "Power now: ${p}W"

or more compact:

power | sed -n 's/^\([[:digit:]]*\).*$/Power now: \1 W/p'
2
  • 1
    AHA! That's what \r does! And that kind of explains \r\n. And makes Apple's \r line endings look pretty odd. Dec 16, 2023 at 10:43
  • 1
    @RichardBarraclough in the end, I bet these choices simply boil down top which line printer manufacturer the university that the guys designing the ancestors of the apple line of computers had. I bet there's someone out there who knows the full story on retrocomputing.stackexchange.com Dec 16, 2023 at 11:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .