Home

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:

chmod +x greetings.sh
#!/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

Next Post: Rules vs Guidelines →

Comments