Skip to content

Instantly share code, notes, and snippets.

@mexomagno
Created April 27, 2023 19:24
Show Gist options
  • Save mexomagno/826f61976bb5275ae4a4ea7f3a12f5e9 to your computer and use it in GitHub Desktop.
Save mexomagno/826f61976bb5275ae4a4ea7f3a12f5e9 to your computer and use it in GitHub Desktop.
Colored ASCII motd

How to creat colored MOTD using linux update-motd

1. Create your ascii art

Create a simple .txt file with your ascii art. Here we create ascii-example.txt:

                                   ....                     
        -++.                         ....::....              
        *%%:                                ...:             
        *%%:                                                 
        *%%:                                                 
  :+#%%#%%%:    =##:     =*#%##+-.   ...    ...    =##.   *#*
 =%%*--=#%%:    +%%-   .#%%=--+%%+    .:.  .:.     *%%:   #%%
 #%%    *%%:    +%%-   =%%-   :%%+     .:..::      *%%:   #%%
 +%%=. :#%%:    +%%-   :%%*: .+%%+      .:::       =%%+. -%%%
  =#%%%%*#%:    +%%-    :*%%%%#*%+       .:         +%%%%%*%%
    .::   .     +%%-       ::.  .                     .:.   .
                +%%-                                         
            -=-=%%%.                                         
           .*#%%#+.

2. Add ANSI color codes with plain normal characters

Useful reference here

Put ANSI color codes everywhere you want. Note we're using plain characters so \033 is literally the characters "backslash zero three three" and not a special character in this context. Example:

image

💡 Don't forget to reset the colors by appending \033[0m.

Note: In this screenshot I'm using Sublime Text 4 with the "Color Highlight" package, with the following user preferences:

```json
  {
    "user":
    {
      "0x_hex_values": true,
      "gutter_icon": "circle",
      "hex_values": true,
      "highlight": true,
      "highlight_values": true,
      "hsl_values": true,
      "hsv_values": true,
      "hwb_values": true,
      "lab_values": true,
      "lch_values": true,
      "named_values": true,
      "rgb_values": true,
      "xterm_color_values": true,
    },
  }
```

3. Try it out in your terminal

cat won't print your ANSI colors unless you actually added special characters to your file, which is not usual or fun to do. A workaround is to use echo together with cat and sed:

  • cat reads the file
  • sed makes sure spaces are taken into account
  • echo interprets ANSI color codes and prints the result.
$ echo -ne $(cat ascii.txt | sed  's/$/\\n/' | sed 's/ /\\a /g')

image

4. Add it to your system's MOTD

  1. Navigate to /etc/update-motd.d/
  2. Copy your ascii-example.txt file into this directory
  3. In the same location, create a new file with the XX-NAME convention for script ordering. In this example, 99-ascii-example. Open it for editing as root
  4. Create script to read and print the file:
    #!/bin/bash
    PATH="/etc/update-motd.d/ascii-example.txt"
    if [ -f "$PATH" ]; then
        echo -ne $(/bin/cat "$PATH" | /bin/sed 's/$/\\n/' | /bin/sed 's/ /\\a /g')
    fi
    ❗ Note that we're using absolute paths to cat and sed or else it wouldnt't work (can you tell me why? I didn't look into it)
  5. Save changes and give execution permissions to your file
    $ sudo chmod +x 99-ascii-example
  6. Try your script
    $ ./99-ascii-example
  7. If all is well, you should see your colored ascii art each time you login.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment