Our first script – Hello World

Since we have learned basic commands in the Linux OS, we will now write our first shell script called hello.sh. You can use any editor of your choice, such as vi, gedit, nano, emacs, geany, and other similar editors. I prefer to use the vi editor:

  1. Create a new hello.sh file as follows:
#!/bin/bash 
# This is comment line 
echo "Hello World" 
ls 
date 
  1. Save the newly created file.

The #!/bin/bash line is called the shebang line. The combination of the characters # and ! is called the magic sequence. The shell uses this to call the intended shell, such as /bin/bash in this case. This should always be the first line in a shell script.

The next few lines in the shell script are self-explanatory:

  • Any line starting ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.