Sample Bash Script Templates

The basic and expanded templates that you can use to begin most of your bash scripts:

template-basic

#!/bin/bash

# scriptname - description of script

scrpt=${0##*/}  # script name

# Display usage if no parameters given

if [[ -z "$@" ]]; then

  echo " $scrpt <input> - description"

  exit

fi

# Text color variables

txtund=$(tput sgr 0 1)          # Underline

txtbld=$(tput bold)             # Bold

bldred=${txtbld}$(tput setaf 1) #  red

bldblu=${txtbld}$(tput setaf 4) #  blue

bldwht=${txtbld}$(tput setaf 7) #  white

txtrst=$(tput sgr0)             # Reset

info=${bldwht}*${txtrst}        # Feedback

pass=${bldblu}*${txtrst}

warn=${bldred}*${txtrst}

ques=${bldblu}?${txtrst}

template

#!/bin/bash

# scriptname - description of script

scrpt=${0##*/}  # script name

# Display usage if no parameters given

if [[ -z "$@" ]]; then

  echo " $scrpt <input> - description"

  exit

fi

# Text color variables

txtred='\e[0;31m'       # red

txtgrn='\e[0;32m'       # green

txtylw='\e[0;33m'       # yellow

txtblu='\e[0;34m'       # blue

txtpur='\e[0;35m'       # purple

txtcyn='\e[0;36m'       # cyan

txtwht='\e[0;37m'       # white

bldred='\e[1;31m'       # red    - Bold

bldgrn='\e[1;32m'       # green

bldylw='\e[1;33m'       # yellow

bldblu='\e[1;34m'       # blue

bldpur='\e[1;35m'       # purple

bldcyn='\e[1;36m'       # cyan

bldwht='\e[1;37m'       # white

txtund=$(tput sgr 0 1)  # Underline

txtbld=$(tput bold)     # Bold

txtrst='\e[0m'          # Text reset

# Feedback indicators

info=${bldwht}*${txtrst}

pass=${bldblu}*${txtrst}

warn=${bldred}!${txtrst}

# Indicator usage

echo -e "${info} "

echo -e "${pass} "

echo -e "${warn} "

Linux Tips

See Also
BASH Scripting Question

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.