Check if internet connection is available in bash script

This bash function uses netcat to test for internet connection (by pinging so it should work on most Linux computers. We use it on our Raspberry Pies because some times if the internet is disconnected, it won’t establish a new connection once the network is back up. To bypass the problem we trigger a reboot which re-initialises network connection.


# Description:  Reboot machine if internet is down

function check_internet_connection() {
    echo -e "GET HTTP/1.0\n\n" | nc 80 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "Online"
        echo "Offline"
        sudo reboot


Add text color to Linux bash scripts

Bash supports coloring text, but I never remember the syntax, so here’s a little cheat sheet. During my little research it also seems you can enter the color codes in two ways as illustrated below:

echo -e "\033[0;32m Hello world!"
echo -e "\e[32m Hello world!"

Some more examples:

Difficult syntax:
\033[0;xxm (where xx is color code)
Easier syntax:
\e[xxm (where xx is color code)

I’m not sure if there’s any reason to choose one or the other, but the latter example is arguably easier to remember.

All color codes:

Reset color   0
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown/Orange 0;33
Light Gray 0;37
Dark Gray 1;30
Light Blue 1;34
Light Green 1;32
Light Cyan 1;36
Light Red 1;31
Light Purple 1;35
Yellow 1;33
White 1;37

You can also do lots of other things in bash, such as blinking text, bold, etc. Take a look at this site for more examples.

If you like to create CLI tools, adding some colors will make it a lot more immersive and easier to read output, like this example:

Bash function to check if it’s friday

Ever wondered if today’s Friday? Yeah me too, every day.

Keep yourself updated with this simple function.


1) Open your profile settings file
nano ~/.bashrc

2) Enter the following function in the bottom

yay() {
    day=$(date +%u)
    if [ "$day" == "5" ];
        echo "YAY, IT'S FRIDAY!!!"
        echo "Not to bring you down, but it's not friday yet :("

3) Reload your config file
. ~/.bashrc

4) Try it out! Just type yay in your terminal window whenever you wonder if it’s Friday or not.

Delete entry from Bash history

First type history to see your bash command history.

Find the entry/command you’d like to erase from history and note the id on the left column.

history -d 1234

Replace 1234 with the actual ID.


$ history
  640  cd ~
  641  ls -la
  642  crontab -e
  643  exit
  644  free -h
  645  df -h
$ history -d 645
$ history
  640  cd ~
  641  ls -la
  642  crontab -e
  643  exit
  644  free -h

As you can see, the df -h command was erased from the history.

Create a daemon for Linux

# !/bin/sh
# /etc/init.d/daemond

# Provides:             daemond
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Skeleton daemon
# Description:          Skeleton daemon

case "$1" in
        echo 'hello world'
        killall daemond -q
      echo "Usage: /etc/init.d/checkconnectiond {start|stop}"
      exit 1

exit 0