Language Selection

English French German Italian Portuguese Spanish

MiniTutor: Shell Colors and Cursor Positions

Filed under
Linux

You can use characters to modify texts and how they are displayed, and also for fun you can draw, create animations, statusbar, progressbar and more.

These commands can be called as escape sequencies because all they use ASCII's ESC (033). They must be send directly to the terminal and you can use 'print', 'printf' or 'echo'. The sequencies use ESC to definy colors and cursos position, they begin with an 'ESC' followed by a '[', and close with a 'm'. In the middle we must add numbers separeted by ';'.

First, we show how to use these commands and sequencies to change default colors displayed by shell. The command format is 'ESC[n1;n2;...m', it means, after the begin '033[' (ESC[) and before the end 'm', we have all numeric instructions. The default is '0' if any number are written, and those numbers indicate text color, background color and video atributes or codes. Those atributes can change text form and how the colors is going to be showed.

The list of text colors is: 30 (black/gray), 31 (red), 32 (green), 33 (brown/yellow), 34 (blue), 35 (purple), 36 (cyan) and 37 (gray/white). The list of background colors is: 40 (black/gray), 41 (red), 42 (green), 43 (brown/yellow), 44 (blue), 45 (purple), 46 (cyan) and 47 (gray/white). The atributes are: 0 (default), 1 (bold), 5 (blinking) and 7 (reverse background and color). There are some differences between colors above as you can see, for example 43 is used to display a brown color, but if you enable bold text the color turnes to yellow.

The number are read following this sequence: background, text color and atributes, for example '40,32,1,5' means black background, green color, bold text and blinking.

You must not forget to enable interpretation of backslash escapes, '-e' option, while using 'echo, for example: echo -e '\033[41m TESTING \033[m'.

Test example: echo -e '\033[40;33;1m Welcome to \033[40;31;1m GoblinX\033[40;33;1mNewsletter \033[m'.

Second, after learn how to change colors, we show how to change the text position. The command to set where display a text is 'ESC[. The common list is: ESC[nA (n lines up and same column), ESC[nB (n lines down, same column), ESC[nC (n columns to the right, same line), ESC[nD (n columns to the left, same line), ESC[nE (n lines down in column 1), ESC[nF (n lines up, column 1), ESC[nG (go to n column, current line) and ESC[n;mH (go to column m and line n).

An example: echo -e '\033c\033[4;7HSaturday\033[AMonday\033[2B\033[DWednesday'

In the above line, '\033c' cleans the screen, '\033[4;7HSaturday' writes Saturday at line 4 column 7, '\033[AMonday' moves the cursos up one line and writes Monday, '\033[2B' moves the cursor two lines down in the same column, and '\033[DWednesday' goes back one column in the same line and writes Wednesday.

There are also more commands to move the cursor before write a text and also others to clear texts and move the screen. The list is: ESC[nJ (n=0, clear until the end of the screen, n=1, clear until the begin of the screen, n=2, clear all screen) ESC[nK (n=0, clear until the end of the line, n=1, clear until the begin of the line, n=2, clear all line), ESC[nM (clear n lines below), ESC[nP (clear n characters in the right side), ESC[nX (clear n characters in the left side and write spaces instead), ESC[n@ (insert n blank spaces), ESC[nL (insert n blank lines), ESC[nS (move the screen n linhas up) and ESC[nT (move the screen n linhas down).

Test example: echo -e '\033c \033[40;33;1m Welcome to \033[4;7H \033[40;31;1m GoblinX\033[1C \033[40;33;1mNewsletter \033[m'

Another example, a counter:
for i in 1 2 3 4 5 6 7 8 9; do echo -ne "\033c \033[G\033[@Counted =\033[11G\033[0K$i"; sleep 1; done; echo

Your shell scripts can inform and also be funny, you just need to let your imagination flyes. This minitutor is heavly inspired by an article in the book 'Programação Shell Linux' written by Julio Cesar Neves.

Minitutor from: GoblinX Minitutors

More in Tux Machines

Mastodon 2.0

About 6 months have passed since April, during which the major mainstream breakthrough of our decentralized social network took place. From 20,000 users to almost a million! What better time to run through a couple examples of what’s been introduced since then? Mastodon is defined by its focus on good user experience, polished design and superior anti-abuse tools. In that vein, the web app has received numerous updates. Using the latest browser features, the web app receives real push notifications, making it almost indistinguishable from a native mobile app. It works faster and looks smoother thanks to many performance and design improvements. Read more

Red Hat: Satellite, OpenShift, Government, SoftBank

  • A Red Hat Satellite tutorial to install an update server
    Is server patch management the best part of your job? Stop reading here. Many IT organizations struggle with OS patching processes. For Red Hat administrators who are willing to invest some initial energy to simplify later tasks, Satellite provides infrastructure lifecycle management, including capabilities for provisioning, reporting and configuration management. To this end, follow this Red Hat Satellite tutorial to set up a simple server for updates. Once we review how to install the basic update server, we'll create one example client.
  • Red Hat updates Gluster storage for OpenShift container apps
    Red Hat bolstered Gluster storage for its OpenShift Container Platform, adding iSCSI block and S3 object interfaces, as well as greater persistent volume density.
  • Red Hat to Cover Open Source Collaboration at Gov’t Symposium; Paul Smith Comments
    Red Hat (NYSE: RHT) is set to hold its annual symposium on federal information technology on Nov. 9 where the company will host discussions on open source collaboration and its potential benefits for government, GovCon Executive reported Oct. 11.
  • Red Hat’s Container Technologies and Knowledge Were Chosen by SoftBank to Embrace DevOps
    Red Hat, Inc. (NYSE: RHT), the world's leading provider of open source solutions, today announced that several of Red Hat’s open source technologies, including Red Hat OpenShift Container Platform, as well as the knowledge of Red Hat Consulting, were chosen by SoftBank Corp (“SoftBank”), a subsidiary of SoftBank Group Corp., to implement DevOps methodology for its Service Platform Division, IT Service Development Division, Information Technology Unit, and Technology Unit, the company’s in-house IT organization. This large, varied organization develops, maintains and operates SoftBank’s IT systems for internal work and operations, supporting 600 diverse systems.
  • Form 4 RED HAT INC For: Oct 17 Filed by: Kelly Michael A
  • Taking a Fresh Look at Red Hat, Inc. (RHT)

Security: Google Play, WPA2, FERC, HackerOne

  • 8 'Minecraft' apps infected with Sockbot malware on Google Play found adding devices to botnet

    Security researchers have discovered that at least eight malware-laced apps on Google Play Store are ensnaring devices to a botnet to potentially carry out distributed denial-of-service (DDoS) and other malicious attacks. These apps claimed to provide skins to tweak the look of characters in the popular Minecraft: Pocket Edition game and have been downloaded as many as 2.6 million times.

  • KRACK Vulnerability: What You Need To Know
    This week security researchers announced a newly discovered vulnerability dubbed KRACK, which affects several common security protocols for Wi-Fi, including WPA (Wireless Protected Access) and WPA2. This is a bad vulnerability in that it likely affects billions of devices, many of which are hard to patch and will remain vulnerable for a long time. Yet in light of the sometimes overblown media coverage, it’s important to keep the impact of KRACK in perspective: KRACK does not affect HTTPS traffic, and KRACK’s discovery does not mean all Wi-Fi networks are under attack. For most people, the sanest thing to do is simply continue using wireless Internet access.
  • FERC sets rules to protect grid from malware spread through laptops
    The Federal Energy Regulatory Commission on Thursday proposed new mandatory cybersecurity controls to protect the utility system from the threat posed by laptops and other mobile devices that could spread malicious software. The standards are meant to "further enhance the reliability and resilience of the nation's bulk electric system" by preventing malware from infecting utility networks and bringing down the power grid, according to the nation's grid regulator.
  • Hack These Apps And Earn $1,000 — Bug Bounty Program Launched By Google And HackerOne
  • Security Vulnerability Puts Linux Kernel at Risk

Smartphone Waste and Tizen News