Hello Script for bash

Hello Script series for Bash. ‘Hello Script’ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.


bash is the most commonly used shell in Linux. That makes the bash scripting language the most popular shell scripting language. OK, maybe after batch. But then again, bash is much more powerful than batch(DOS scripting language). If want to learn bash, I will recommend this tutorial – Advanced Bash Scripting

Officially, I hate bash. I use perl or other similar high level language to create shell script. I use bash only for the simplest scripts. But even I admit that bash has its uses. So, here is the hello script for bash…

Hello Script

# Printing(IO)
echo "Hello World"

# Variables, concatenation
echo "Hello, " $name " - welcome to " $year

#If,else conditions
if [ $year -gt 2008 ]; then
	echo "Welcome to the future - yes, we have flying cars!"
elif [ $year -lt 2008 ]; then
	echo "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!"
	echo "Anything wrong with your time machine? You have not gone anywhere, kiddo."

# If you are using anything after this, consider using a high level language.
# For loop
for i in 1 2 3
	echo $i ") Hi there!"

#Numerical Array, foreach
rules[0]="Do no harm"
rules[2]="Continue Living"

for ((i=0; i < 3; i++))
	echo "Rule" `expr $i + 1` ":" ${rules[$i]}

#A While Demo
keys=(hello foo lorem)

while [ $i -lt 3 ]
	echo ${keys[$i]}
	i=`expr $i + 1`

# Function, argument, return, call
hello () {
	myname=$1  #First argument.
	echo "Hello" $myname
hello "Binny"

# File IO
# File reading
contents=`cat Hello.sh` #For some reason, I'm losing all the \n's in the file.
echo "Hello has `echo $contents|wc -m` chars" # Or wc -m Hello.sh

# Writing to a file
echo "Hello World from shell script" > /tmp/hello.txt
# Command Executing

# Regular Expressions
string="Hello World"
evil=`echo $string | grep '^Hell'`
if [ "$evil" != "" ]; then
	echo "Yup - its evil"
echo "Hello World" | sed -e 's/l//g' #Will return Heo Word

# http://tldp.org/LDP/abs/html/

And, by the way, this will work only in Linux – or if you installed cygwin in your Windows system. Use sh <File_Name> to execute the above code.


  1. First of all, please consider posting normal quotes in code. Funny html-quotes may look nice, but bash does not understand them and so we have to change them by hand (which is rather annoying 🙂

    Now to the actual code ..

    If you know how many iterations you need, the “for ((i=0; i contents=`cat Hello.sh` #For some reason, I’m losing all the \n’s in the file.
    > echo “Hello has `echo $contents|wc -m` chars” # Or wc -m Hello.sh

    contents=$(< Hello.sh); # You won a “useless use of cat”-award
    echo “Hello has $(wc -m << string=”Hello World”
    > evil=`echo $string | grep ‘^Hell’`
    > if [ “$evil” != “” ]; then
    > echo “Yup – its evil”
    > fi

    string=”Hello World”
    if grep “^Hell” << echo “Hello World” | sed -e ’s/l//g’ #Will return Heo Word

    echo “${string//l/}” # assuming $string is still valid

    Cheers, Emil

  2. @Emil
    > First of all, please consider posting normal quotes in code
    I am innocent of this crime. I used normal single and double quote in the code. But WordPress converts them into those fancy html quotes.

    And thanks for the other pointers – especially…
    > contents=$(< Hello.sh); This will come in handy.

Comments are closed.