bash hello world

“Hello World” in bash

Let’s suppose that you have just opened Linux terminal for the first time in your life. Your wish is to write nothing but bash “hello world” type of thing. As this command is one-liner, you can simply do this:

[root@linux ~]# echo "Hello World"
Hello World
[root@linux ~]#

“echo” does nothing more but displays line of text.

Typically, more advanced problems will require ability to run not just one command, but sequence of them. You can do that by separating several commands with semicolon:

[root@linux ~]# echo "Hello World"; echo "foo bar"
Hello World
foo bar
[root@linux ~]#

So what is script?

If there were only few commands to be run in sequence, this semicolon approach might be just enough. However, longer sequences would become both not easy to read and hard to edit. In order to solve it, you can put them into file, save it and then execute it. Output of such file would be the very same result as if you typed commands directly into CLI and separate them with semicolon. Such file is called a “shell script”. Yes, it’s nothing more but a sequence of commands.

As we already stated in the beginning, it’s your first time in CLI, so you don’t really know how to edit files here. There are few ways how to do it:

  • use one of the text editors: vi / vim / emacs / nano, etc.
  • write script outside of your Linux terminal and copy-paste / upload it

First option might require some time learning it, especially if you choose vi / vim. However, vi editor is a must to know, if speaking about administrating servers – you will find this editor in every *nix machine, it will be there for you when no other editor is available. So learning how to use this editor will really bring you benefit.

bash hello world – your first script

Given that you found your way into creating / editing files, rewriting the above example to make it a “script” can be done as simple as this:

#!/bin/bash
echo "Hello World"
echo "foo bar"

First line is a special line called “shebang“. It’s not mandatory, but get used to it – this is your way to tell Linux, which interpreter should be used to run your script.

Save your script. By the way, file extensions doesn’t matter much in Linux, so you could name your script anything. But to keep things neat, name them “.sh” – this way you’ll know that those files are scripts without even opening them, especially if you’ll have many different files around.

Try to run your script. The most common (and convenient) way to do it is to add “./” in front of your file name:

[root@linux ~]# vi hello.sh
[root@linux ~]# cat hello.sh
#!/bin/bash
echo "Hello World"
echo "foo bar"
[root@linux ~]# ./hello.sh
-bash: ./hello.sh: Permission denied
[root@linux ~]#

You got an error which is absolutely normal at this point. Linux has 3 type of permissions regarding files: r, w, x (read, write, execute) and 3 types of ownership (user – owner, group, other). If you check your newly created file, it will have “x” permission missing:

[root@linux ~]# ls -l hello.sh
-rw-r--r-- 1 root root 46 Oct  5 14:05 hello.sh
[root@linux ~]#

You can change it with “chmod” command like this:

[root@linux ~]# chmod +x hello.sh
[root@linux ~]# ls -l hello.sh
-rwxr-xr-x 1 root root 46 Oct  5 14:05 hello.sh
[root@linux ~]#

Now it’s executable:

[root@linux ~]# ./hello.sh
Hello World
foo bar
[root@linux ~]#

Congratulations – you learned how to write your first bash script!