Happy Birthday Bash Script

Following on from the PHP script to print happy birthday I wanted do the same in a bash script. I don't really use bash for much more than stringing together commands so I had to figure out how to do loops and if statements using the simple bash syntax. I also wanted to pass the name of the person as an argument, rather than hard code it into the script. This is what I came up with.

#!/bin/bash

output=''
for i in {1..4}
do
  output=$output"Happy birthday "

  if [ $i -eq 3 ]
  then
    output=$output"dear $1\n"
  else
    output=$output"to you\n"
  fi
done
 
echo -e $output

Save the file as happy.sh or similar and run it in the following way (Name is the argument that we pass to the script).

$ ./happy.sh Name

Add new comment

The content of this field is kept private and will not be shown publicly.