Hello World using Bash Script
To print hello world using your bash script. Run the following in your Bash terminal:
echo "Hello World"
echo
is a Bash builtin command that writes the arguments it receives to the standard output. It appends a newline to the output, by default.
Creating a script file(.sh)
You can run a bash script from a .sh
file also.
To create a script file:
- [x] Create a file. for e.g.,
greetings.sh
- [x] Make the file executable by running the command
chmod +x greetings.sh
- [x] Add the necessary code for the script in the file
#!/bin/bash
echo "Hello World"
Script file explanation
The first line of the script will start with the shebang(#!) which is followed by the shell. Rest of the file contains the script that is executed inside the shell mentioned. For example, in the above code #!/bin/bash
say that the file to be executed as /bin/bash greetings.sh
. The actual script here is echo "Hello World"
.
Script file execution
If you are in your bash shell you can execute the script file just using the file name. For example:
./greetings.sh
Other commonly used commands to execute scripts are:
/bin/bash hello-world.sh
bash hello-world.sh
# assuming /bin is in your $PATH
sh hello-world.sh
Resources
Last Updated on
Comments