bash christmas tree

Jump toUpdate content

Using Bash to display a Christmas tree

Published on 26 November 2019
  • bash
  • christmas
  • random
  • tree
  • code

Bash Scripting - Overview

Bash (Bourne again shell) is an improved version of Sh (Bourne shell) and is available by default on Linux and MacOS operating systems. A shell provides a command line interface (CLI) to interact with the operating system of a computer. It interprets commands in plain text format and passes the information to the operating stems to launch an action.

A bash script is a plain text file containing a series of commands. These commands can be a mixture of commands you would normally type by yourself on the shell (like ls or cd) and other commands you would normally not type by yourself as they can be more complex. Any command available on the shell can be used in a bash script - and vice versa.

No special knowledge is required to write bash scripts, as they are plain text files containing the series of commands required to run a specific task. Instead of typing then manually on the shell, you write them in a script and run the script afterwards. This can be very useful for system administration tasks as you can automatize tasks. The common file extension for bash scripts is *.sh.


We recommend you follow this tutorial using a Learning Instance.

Getting Started

  1. Connect to your Instance using SSH:
ssh root@<Instance Public IP>
  1. Update the apt packet cache and the software already installed on the Instance:
    apt update && apt upgrade -y
  2. Create a new bash script and open it in a text editor, for example nano:
    nano xmas.sh
  3. Copy the following bash code into the script:
    # The following line tells the shell what program to interpret the script with
    # tput is a command to manipulate the terminal, it can be used to change the color of text, apply effects, and generally brighten things up.
    trap "tput reset; tput cnorm; exit" 2
    tput civis
    col=$(($(tput cols) / 2))
    # Set the text color to green to write the tree
    tput setaf 2; tput bold
    # Write the tree
    for ((i=1; i<40; i+=2))
    tput cup $lin $col
    for ((j=1; j<=i; j++))
    echo -n \*
    let lin++
    let col--
    ## Set the color to brown for the trunk
    tput sgr0; tput setaf 130
    # Write the Trunk in three lines
    for ((i=1; i<=3; i++))
    tput cup $((lin++)) $c
    echo 'mWm'
    # Write a greeting
    tput setaf 93; tput bold
    tput cup $lin $((c - 15)); echo SCALEWAY wishes you Merry Christmas
    tput cup $((lin + 1)) $((c - 11)); echo And a Happy New Year 2023
    let c++
    # Configure lights and decorations
    while true; do
    for ((i=1; i<=35; i++)) {
    # Turn off the lights
    [ $k -gt 1 ] && {
    tput setaf 2; tput bold
    tput cup ${line[$[k-1]$i]} ${column[$[k-1]$i]}; echo \*
    unset line[$[k-1]$i]; unset column[$[k-1]$i] # Array cleanup
    li=$((RANDOM % 9 + 10))
    co=$((RANDOM % (li-2) * 2 + 1 + start))
    tput setaf $color; tput bold # Switch colors
    tput cup $li $co
    echo o
    k=$((k % 2 + 1))
  4. Paste the copied file and press CTRL and s to save.
  5. Exit nano by pressing CTRL and x.
  6. Make the script executable using the chmod command (necessary because, by default, plain text files are not executable):
    chmod +x xmas.sh

Running the script

  1. Run the script from your terminal by typing the following code:


    The script writes an animated tree on the terminal window:

  2. Exit the script by pressing CTRL + c.