A “Hello, World!” bash shell script is a bash program that outputs “Hello, World!” to a user. This script illustrates the basic syntax of a bash shell scripting language for a working program. It is your very first program when you are new to a bash shell scripting on Linux and Unix-like systems.
How to write hello world bash shell script
The procedure is as follows:
- Create a new file called hello.sh using a text editor such as nano or vi:
nano hello.sh - Add the following code:
#!/bin/bash
echo "Hello World" - Set the script executable permission by running chmod command:
chmod +x hello.sh - Run or execute the script using following syntax:
./hello.sh
OR
sh hello.sh
OR
bash hello.sh
Improving Hello World script
Let us create a program called update-hello.sh as follows:
#!/bin/bash # Usage: Hello World Bash Shell Script Using Variables # Author: Vivek Gite # ------------------------------------------------- # Define bash shell variable called var # Avoid spaces around the assignment operator (=) var="Hello World" # print it echo "$var" # Another way of printing it printf "%s\n" "$var"
Run it as follows:
chmod +x update-hello.sh
./update-hello.sh
Where,
- First line of the script (#!/bin/bash) is shebang. It tells the Linux/Unix how to run the script.
- Each shell comments start with #.
- Declaring a variable: Variable_Name="Values_here".
Next create a program named hello2.sh to display current date and computer/system name as follows:
#!/bin/bash var="Hello World" # Run date and hostname command and store output to shell variables now="$(date)" computer_name="$(hostname)" # # print it or use the variable # Variable names are case sensitive $now and $NOW are different names # echo "$var" echo "Current date and time : $now" echo "Computer name : $computer_name" echo ""
Run it as follows:
chmod +x hello2.sh
./hello2.sh
Reading a value from input
Our final hello world program reads input from the keyboard using the read command. Create a bash shell script named hello-input.sh:
$ nano hello-input.sh
Append the following code:
#!/bin/bash # Clear the screen clear # Read input using read command read -p "May I know your name please? " name echo "Hello $name, let us be friends" echo ""
Save and close the file. Run it as follows:
$ chmod +x hello-input.sh
$ ./hello-input.sh
Running your shell script in “debug” mode
Pass the -x and -v option:
bash -x hello-input.sh
bash -v hello-input.sh
bash -x -v hello-input.sh
Conclusion
You learned how to write “Hello World” shell script and run it on Linux or Unix-like systems. For more info see Linux Shell Scripting Tutorial and bash command man page:
man bash
help read
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 2 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
This was the best and really easy to follow tutorials. Thank you!
This was the best and really easy to follow tutorials. Thank you!