Search code examples
macosvariableszshinvisible

zsh remove invisible character


I have this relatively simple script that keeps failing. Now I have noticed that two ** have crept in. How can I remove them?

#!/bin/zsh

checkupdate='macOS Ventura'
macos='macOS Ventura'

echo -n $checkupdate | wc -m
echo -n $macos | wc -m

echo "$checkupdate" | tr -dc '[:print:]' | od -c
echo "***************"
echo "$macos" | tr -dc '[:print:]' | od -c

if [[ $checkupdate == $macos ]]; then
  echo "ok"
else
  echo "bad"
  exit 1
fi

Result:

sudo ./test10.sh
      13
      13
0000000    m   a   c   O   S      **   V   e   n   t   u   r   a
0000016
***************
0000000    m   a   c   O   S       V   e   n   t   u   r   a
0000015
bad

before the word "Ventura" are apparently these two **

Background information (Edit 21.11.2023): The first "macOS Ventura" comes from the command softwareupdate -l. I copied it one to one from there. It should then be copied automatically in the finished script, but without these "special characters"


Solution

  • I have succeeded in removing these two ** This is the command where several ** are included

    $ softwareupdate -l| od -c
    0000000    S   o   f   t   w   a   r   e       U   p   d   a   t   e
    0000020    T   o   o   l  \n  \n   F   i   n   d   i   n   g       a   v
    0000040    a   i   l   a   b   l   e       s   o   f   t   w   a   r   e
    0000060   \n   S   o   f   t   w   a   r   e       U   p   d   a   t   e
    0000100        f   o   u   n   d       t   h   e       f   o   l   l   o
    0000120    w   i   n   g       n   e   w       o   r       u   p   d   a
    0000140    t   e   d       s   o   f   t   w   a   r   e   :  \n   *
    0000160    L   a   b   e   l   :       S   a   f   a   r   i   1   7   .
    0000200    1   V   e   n   t   u   r   a   A   u   t   o   -   1   7   .
    0000220    1  \n  \t   T   i   t   l   e   :       S   a   f   a   r   i
    0000240    ,       V   e   r   s   i   o   n   :       1   7   .   1   ,
    0000260        S   i   z   e   :       1   6   2   0   6   3   K   i   B
    0000300    ,       R   e   c   o   m   m   e   n   d   e   d   :       Y
    0000320    E   S   ,      \n   *       L   a   b   e   l   :       m   a
    0000340    c   O   S      **   V   e   n   t   u   r   a      **   1   3
    0000360    .   6   .   2   -   2   2   G   3   2   0  \n  \t   T   i   t
    0000400    l   e   :       m   a   c   O   S      **   V   e   n   t   u
    0000420    r   a      **   1   3   .   6   .   2   ,       V   e   r   s
    0000440    i   o   n   :       1   3   .   6   .   2   ,       S   i   z
    0000460    e   :       4   9   2   9   7   9   K   i   B   ,       R   e
    0000500    c   o   m   m   e   n   d   e   d   :       Y   E   S   ,
    0000520    A   c   t   i   o   n   :       r   e   s   t   a   r   t   ,
    0000540       \n   *       L   a   b   e   l   :       m   a   c   O   S
    0000560        S   o   n   o   m   a       1   4   .   1   .   1   -   2
    0000600    3   B   8   1  \n  \t   T   i   t   l   e   :       m   a   c
    0000620    O   S       S   o   n   o   m   a       1   4   .   1   .   1
    0000640    ,       V   e   r   s   i   o   n   :       1   4   .   1   .
    0000660    1   ,       S   i   z   e   :       6   3   7   0   5   4   9
    0000700    K   i   B   ,       R   e   c   o   m   m   e   n   d   e   d
    0000720    :       Y   E   S   ,       A   c   t   i   o   n   :       r
    0000740    e   s   t   a   r   t   ,      \n
    0000751
    

    I have constructed this command:

    softwareupdate -l | LC_ALL=C tr -cd '\11\12\15\40-\176'
    

    It works perfectly in my test (with the 8 MacBooks) so far. A short explanation:

    • LC_ALL=C: Sets the locale to "C", i.e. only ASCII characters are taken into account to ensure consistent results.
    • tr -cd '\11\12\15\40-\176': Removes all characters that are not tabs, line feeds, carriage returns or printable ASCII characters between spaces and tilde.

    Output:

    $ softwareupdate -l | LC_ALL=C tr -cd '\11\12\15\40-\176' | od -c
    0000000    S   o   f   t   w   a   r   e       U   p   d   a   t   e
    0000020    T   o   o   l  \n  \n   F   i   n   d   i   n   g       a   v
    0000040    a   i   l   a   b   l   e       s   o   f   t   w   a   r   e
    0000060   \n   S   o   f   t   w   a   r   e       U   p   d   a   t   e
    0000100        f   o   u   n   d       t   h   e       f   o   l   l   o
    0000120    w   i   n   g       n   e   w       o   r       u   p   d   a
    0000140    t   e   d       s   o   f   t   w   a   r   e   :  \n   *
    0000160    L   a   b   e   l   :       S   a   f   a   r   i   1   7   .
    0000200    1   V   e   n   t   u   r   a   A   u   t   o   -   1   7   .
    0000220    1  \n  \t   T   i   t   l   e   :       S   a   f   a   r   i
    0000240    ,       V   e   r   s   i   o   n   :       1   7   .   1   ,
    0000260        S   i   z   e   :       1   6   2   0   6   3   K   i   B
    0000300    ,       R   e   c   o   m   m   e   n   d   e   d   :       Y
    0000320    E   S   ,      \n   *       L   a   b   e   l   :       m   a
    0000340    c   O   S   V   e   n   t   u   r   a   1   3   .   6   .   2
    0000360    -   2   2   G   3   2   0  \n  \t   T   i   t   l   e   :
    0000400    m   a   c   O   S   V   e   n   t   u   r   a   1   3   .   6
    0000420    .   2   ,       V   e   r   s   i   o   n   :       1   3   .
    0000440    6   .   2   ,       S   i   z   e   :       4   9   2   9   7
    0000460    9   K   i   B   ,       R   e   c   o   m   m   e   n   d   e
    0000500    d   :       Y   E   S   ,       A   c   t   i   o   n   :
    0000520    r   e   s   t   a   r   t   ,      \n   *       L   a   b   e
    0000540    l   :       m   a   c   O   S       S   o   n   o   m   a
    0000560    1   4   .   1   .   1   -   2   3   B   8   1  \n  \t   T   i
    0000600    t   l   e   :       m   a   c   O   S       S   o   n   o   m
    0000620    a       1   4   .   1   .   1   ,       V   e   r   s   i   o
    0000640    n   :       1   4   .   1   .   1   ,       S   i   z   e   :
    0000660        6   3   7   0   5   4   9   K   i   B   ,       R   e   c
    0000700    o   m   m   e   n   d   e   d   :       Y   E   S   ,       A
    0000720    c   t   i   o   n   :       r   e   s   t   a   r   t   ,
    0000740   \n
    0000741