Create a simple bash/shell script

Step by Step Guide

  1. Make sure you are working on a shell environment i.e Linux Terminal, Command Line, Bash etc.
  2. Navigate to a directory (using “cd [path]”) where you want to create the script or skip this step if you want to create it in the default directory ([user]/home/).
  3. Enter the following command BUT change [filename] to a suitable name:
    vi [filename].sh
  4. Upon pressing Return/Enter, you will see the Vi Editor Interface in your terminal.
  5. Press the "Insert" key ONCE, to go into insert mode and be able to type.
  6. Then enter the following content in the file:
    #!/bin/bash
    echo "Enter First Value"
    read value1
    echo "Enter Second Value"
    read value2
    if [ $value1 -gt $value2 ]
    then
    echo "First Value is greater"
    elif [ $value2 -gt $value1 ]
    then
    echo "Second Value is greater"
    else
    echo "Both Values are same"
    fi
  7. When you are done, press "Esc" then press ":", "w", "q" in this same order and press Return/Enter. This will save the file and exit the editor.
  8. Now enter the following command to execute your script. Make sure you are currently in the directory where the file exists otherwise, use the second command where [path] refers to the full path/directory of the file:
    bash [filename].sh


    OR

    bash [path]/[filename].sh
  9. Congrats 🎉You created your own shell script and executed it!