# Shell Script formatting broken due to whitespace trimming

**URL:** <https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772>\
**Category:** Bugs or Unexpected Behavior\
**Tags:** widget\
**Created:** [August 15, 2023, 7:03pm UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772 "2023-08-15T19:03:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yury\_Ershov](https://community.folivora.ai/user_avatar/community.folivora.ai/yury_ershov/32/17703_2.png) [@Yury\_Ershov](https://community.folivora.ai/u/Yury_Ershov)\
**Post date:** [August 15, 2023, 7:03pm UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/1 "2023-08-15T19:03:10Z")

</div>

I use a two-line shell widget that displays a small table via pre-formatting the output with whitespaces.  
It used to work well until a recent update where the whitespaces at the beginning and end of the output got trimmed, thus breaking the formatting.

![image](https://community.folivora.ai/uploads/default/original/3X/2/2/22493c098b5cb02d60f50afe31a9c00088c3bc6e.png)

Note that the whitespace is lacking at the beginning of the first line and the end of the second line.

I understand that this behavior is useful for many widgets. This could be resolved via an additional option on the widget whether to trim whitespaces or not.

**Device information:**

- Type of Mac: MacBook Pro M1 16" and MacBook Air M2.
- macOS version: Ventura 13.4.1 (22F82)
- BetterTouchTool version: 4.075 (2291)

---

<div class="post-metadata">

**Author:** ![Yury\_Ershov](https://community.folivora.ai/user_avatar/community.folivora.ai/yury_ershov/32/17703_2.png) [@Yury\_Ershov](https://community.folivora.ai/u/Yury_Ershov)\
**Post date:** [August 15, 2023, 7:17pm UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/2 "2023-08-15T19:17:47Z")

</div>

Update:

I managed to restore formatting by inserting one of invisible unicode spaces from [here](https://invisible-characters.com/) (but most of them still get trimmed).

---

<div class="post-metadata">

**Author:** ![Andreas\_Hegenberg](https://community.folivora.ai/user_avatar/community.folivora.ai/andreas_hegenberg/32/39221_2.png) [@Andreas\_Hegenberg](https://community.folivora.ai/u/Andreas_Hegenberg)\
**Post date:** [August 15, 2023, 7:27pm UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/3 "2023-08-15T19:27:01Z")

</div>

Can you post your script? I can't think of any trimming setting that was recently changed

---

<div class="post-metadata">

**Author:** ![Yury\_Ershov](https://community.folivora.ai/user_avatar/community.folivora.ai/yury_ershov/32/17703_2.png) [@Yury\_Ershov](https://community.folivora.ai/u/Yury_Ershov)\
**Post date:** [August 15, 2023, 7:36pm UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/4 "2023-08-15T19:36:48Z")

</div>

Yeah it's this one: [cpu top 2 (and top 30) (toolbar widget preset)](https://community.folivora.ai/t/cpu-top-2-and-top-30-toolbar-widget-preset/23890)

(note the screenshot from an earlier version)

The script being this:

```auto
ps -A -o %cpu,comm | perl -naE '
BEGIN{$n=$c=0};
chomp;
$n+=$F[0];
$c+=$F[0] if /Chrome\.app/;
$p = m{/([^/]+)\.app/} ? $1 : m{/([^/]+)$} ? $1 : m{^\s*[\d.]+\s++(.*)} ? $1 : $_;
$p{$p}+=$F[0];
END{
  print
    join "\n",
    map {sprintf "%16s %5.1f", substr($_,0,16), $p{$_}}
    (sort {$p{$b} <=> $p{$a}} keys %p)[0,1];
}
'

```

I think it might be because of some changes to 3rd party libraries where they attempt to center the line and automatically trim spaces when doing that.

---

<div class="post-metadata">

**Author:** ![Yury\_Ershov](https://community.folivora.ai/user_avatar/community.folivora.ai/yury_ershov/32/17703_2.png) [@Yury\_Ershov](https://community.folivora.ai/u/Yury_Ershov)\
**Post date:** [August 21, 2023, 10:28am UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/5 "2023-08-21T10:28:54Z")

</div>

One more observation:

This one is wrongly formatted:

```auto
" 123 \n 456 "

```

This one is formatted well:

```auto
"\xc2\xad 123 \n 456 \xc2\xad"

```

(`"\xc2\xad"`) is an invisible space in UTF8

---

<div class="post-metadata">

**Author:** ![Andreas\_Hegenberg](https://community.folivora.ai/user_avatar/community.folivora.ai/andreas_hegenberg/32/39221_2.png) [@Andreas\_Hegenberg](https://community.folivora.ai/u/Andreas_Hegenberg)\
**Post date:** [August 22, 2023, 6:53am UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/6 "2023-08-22T06:53:53Z")

</div>

BTT is indeed trimming the white space at the beginning and the end (but has always done so).

I'll add an option to disable this.

---

<div class="post-metadata">

**Author:** ![Andreas\_Hegenberg](https://community.folivora.ai/user_avatar/community.folivora.ai/andreas_hegenberg/32/39221_2.png) [@Andreas\_Hegenberg](https://community.folivora.ai/u/Andreas_Hegenberg)\
**Post date:** [August 24, 2023, 9:07pm UTC](https://community.folivora.ai/t/shell-script-formatting-broken-due-to-whitespace-trimming/32772/7 "2023-08-24T21:07:26Z")

</div>

The new alpha 4.188 now has an option to trim or not to trim whitespace:

 ![image](https://community.folivora.ai/uploads/default/original/3X/3/b/3b333f0770fd80efbd8daed119aea48754f97b3b.jpeg)
