\e[0m turns off the yellow command and returns to whatever the previous color was.This is how ANSI color escape codes operate. The * character runs together with the m. The 1 indicates bold text, and the 33 represents yellow. \e[1 33m begins the “bold, yellow foreground color” sequence.This is very messy in the script, but Bash knows how to display it. Run the escape codes together with the star.The escape codes themselves are not printed. This usually aligns a character correctly though it will through off successive characters on the same line. To align the star at the top of the tree while using ANSI escape codes, place the escape code backslash where the star would normally appear.Looking good! The star is yellow while to tree is green. Important: Make sure to add the -e option to echo in order to interpret the backslashes used by the escape codes! Without -e, echo will show the messy ANSI code. We will not use it anymore because ANSI color escape codes remove its effect. For now, let’s color the star yellow while leaving the rest of the tree green. Blinking text is possible, so we can make the star blink if we want to. The off code \e[0m is always the same, but the on code can vary depending upon the color and special effects, like blinking or underline text. The idea behind ANSI color escape codes is that we turn the color “on” with a special character sequence like \e[1 32m and turn it “off” with \e[0m. It is nothing new (even DOS 5.0 supported color escape codes), and plenty guides exist, such as this one. To achieve this, we will use ANSI escape codes to turn colors on and off at specific characters (parts of the tree).Īn ANSI escape code is a standard way to alter colors in a terminal. This is good, but why stop here? Our tree contains a star and ornaments, so let’s color those too! You should see a simple ASCII art Christmas tree. Set the executable file permission (or use bash to execute it), and execute the script from a command line. Notice the extra backslashes added? We need to escape each backslash that is part of the tree with an extra backslash because backslashes begin escape sequences in Bash, and we want the tree backslashes to appear. Copy and paste the ASCII art tree into the file between the double quotes of an echo statement as shown. It is quicker and cleaner at the command line to execute a Bash script. The idea is to use this file for experimentation. I named this file tree.sh, but feel free to name it anything you like. Let’s use a tree like this (complete with a star on the top!): * The more special characters your tree contains, the more work involved. Preferably, use a simple tree because we will need to modify any problematic characters, such as backslashes. Display a Christmas tree using ASCII artįind any ASCII art Christmas tree that you like. Just for fun! To help add some festive Christmas cheer to a Bash terminal this year, we can write a Bash script that will display a small Christmas tree using ANSI color escape sequences to alter tree, star, and ornament colors. The flexibility of Bash allows us to create some interesting text effects at the command line. Let’s make a simple multi-colored Christmas tree using ASCII art!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |