Create a simple bash/shell script
Step by Step Guide
- Make sure you are working on a shell environment i.e Linux Terminal, Command Line, Bash etc.
- 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/).
- Enter the following command BUT change [filename] to a suitable name:
vi [filename].sh
- Upon pressing Return/Enter, you will see the Vi Editor Interface in your terminal.
- Press the "Insert" key ONCE, to go into insert mode and be able to type.
- 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
- 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.
- 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
- Congrats 🎉You created your own shell script and executed it!