Coloured BASH Output Using TPUT

From my previous post, found here, you can see that I have formatted the text to be a specific colour depending on what sort of output I get. So for errors I make the text red and for successful messages I make the text green.

This is easy to implement into BASH scripts and a lot of other formatting can be applied as well. In this post, I will be covering the colorization (probably not a word), underlining, bold text and resetting the changes.

Colour Possibilities:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White
$(tput setaf 1)TEXT HERE

Bold Text:

#Starts bold characters

$(tput bold)TEXT HERE

#Ends bold characters

$(tput sgr0)TEXT HERE

I would just like to add here that tput sgr0 removes all formatting and returns text to the default style and colour.


#Starts underlining

$(tput smul)TEXT HERE

#Ends underlining

$(tput rmul)TEXT HERE

Below is a full script which includes all the possible combinations of the examples above, apart from black text.


echo "
regular bold underline
$(tput setaf 1)Text $(tput bold)Text $(tput sgr0)$(tput setaf 1)$(tput smul)Text$(tput rmul)
$(tput setaf 2)Text $(tput bold)Text $(tput sgr0)$(tput setaf 2)$(tput smul)Text$(tput rmul)
$(tput setaf 3)Text $(tput bold)Text $(tput sgr0)$(tput setaf 3)$(tput smul)Text$(tput rmul)
$(tput setaf 4)Text $(tput bold)Text $(tput sgr0)$(tput setaf 4)$(tput smul)Text$(tput rmul)
$(tput setaf 5)Text $(tput bold)Text $(tput sgr0)$(tput setaf 5)$(tput smul)Text$(tput rmul)
$(tput setaf 6)Text $(tput bold)Text $(tput sgr0)$(tput setaf 6)$(tput smul)Text$(tput rmul)
$(tput setaf 7)Text $(tput bold)Text $(tput sgr0)$(tput setaf 7)$(tput smul)Text$(tput rmul)

I know it looks quite horrible in the source code but this is what the output looks like:

Full possibilities


Linux Service Restart Script

I required a way to easily restart some common services on my Raspberry Pi running Plex since I kept forgetting whether I needed to use the:

service NAME restart

syntax, or the:

systemctl restart NAME

syntax for the services.

So I created a bash (.SH) file that would allow me to chose which service to restart without having to remember the different commands. Below are my requirements:

  • Give a list of services
  • read which service to restart
  • Restart service
  • restart script

You can see my script below:


restart_script () {
 bash /home/pi/restartservices

echo "$(tput setaf 3)Following services available:"
echo ""


for SERVICE in "VSFTPD" "Plex" "VNC Server" "LightDM"
 echo "$(tput setaf 3)($NUMBER)$(tput setaf 7)$SERVICE"

echo ""
echo "$(tput setaf 3)(E)xit:"

read -p "$(tput setaf 3)Chose a service to restart:" READINPUT

if [ $READINPUT = "1" ]
 echo "$(tput setaf 3)Restarting VSFTPD Service...."
 sudo service vsftpd restart
 echo "$(tput setaf 2)Success!"
elif [ $READINPUT = "2" ]
 echo "$(tput setaf 3)Restarting Plex Service...."
 sudo service plexmediaserver restart
 echo "$(tput setaf 2)Success!"
elif [ $READINPUT = "3" ]
 echo "$(tput setaf 3)Restarting VNC Server...."
 sudo service vncserver restart
 echo "$(tput setaf 2)Success!"
elif [ $READINPUT = "4" ]
 echo "$(tput setaf 3)Restarting LightDM Service...."
 sudo systemctl restart lightdm
 echo "$(tput setaf 2)Success!"
elif [ $READINPUT = "E" ] || [ $READINPUT = "e" ]
 echo "$(tput setaf 3)Exiting Script"
 exit 1

Rather than having to type in the name of the service I wanted to restart, I used a for loop to add a number to each entry in the services list. This can be found from line 14 to line 19.

Then to run my script from a terminal session, I simply type “./restartservices”


Server Reboot Script

Running a little low on content this last few months, plus I’ve been busy with other work stuff.

I had the requirement to create a PowerShell script that would get the uptime of a server and then decide whether or not the server needed rebooting.

I also wanted the script to randomize the reboot of the servers, that way if there are multiple servers that need rebooting at once, they don’t cause a power spike or resource issues on the hosts. I did this by creating a random number between 1 and 5 and then if the number equals 5, the server is rebooted. If not then the server isn’t rebooted.

This is the script that I ended up with and what is currently being tested:

$loglocation = "C:\scripts\reboot\log"
$dateforfile = Get-Date

$lastbootuptime = Get-WmiObject win32_operatingsystem
$uptime = (Get-Date) - ($lastbootuptime.converttodatetime($lastbootuptime.lastbootuptime))
$uptimeindays = $uptime.days

$randomnumber = Get-Random -Minimum 1 -Maximum 6

if ($uptimeindays -ge "14"){

 Add-Content -Path "$loglocation\$env:COMPUTERNAME.txt" -Value @"
Server restarted at:
This was an immediate shutdown as the server had been up for $uptimeindays days

 Restart-Computer -Force

}elseif ($uptimeindays -lt "14" -and $uptimeindays -ge "7"){

    if ($randomnumber -eq "5"){

        Add-Content -Path "$loglocation\$env:COMPUTERNAME.txt" -Value @"
Server restarted at :
This was a random restart as uptime was only $uptimeindays days
        Restart-Computer -Force

        Add-Content -Path "$loglocation\$env:COMPUTERNAME.txt" -Value @"
Server NOT restarted
This was not randomly restarted. Uptime is currently $uptimeindays days. Random number was $randomnumber

Add-Content -Path "loglocation\$env:COMPUTERNAME.txt" -Value @"
No restart required
No restart required since uptime is only $uptimeindays days

The first time I created this script and set it up as a scheduled task, nothing happened. Turns out that I needed the -Force parameter in order for the server to be rebooted.

This will later be used in a group policy without the log creating as that is only necessary in the testing stage.