GNU/Linux Programmer's Solver - Bash

Hello World program in Bash. Basic structure, comments and string literals
PROGRAMMING BASH HELLO WORLD SHELL PROGRAMMING
#!/bin/sh

# This is a comment

echo "Hello World!"

cat <<EOF
-------------------------------
This is a multiline
character string
-------------------------------
EOF

<<EOF
HEREDOCS can also be 
used to incluide multiline
comments
EOF

: '
Another way to include comments
is using the  : operator which
does nothing
'

Bash is one of the most popular command interpreters, in part due to the large number of functions it has added over the years compared to the standard shell.

Compared to the other languages we are seeing, the shell has much lower expressive power, however, the chances that at some point you will have to write a program for a shell (what is known as a Shell Script) are very high.

Shell scripts usually use the .sh extension (derived from SHell obviously).

DID YOU KNOW…

Sometimes you will see shell scripts with other extensions such as bash. In these cases, the extension usually indicates the type of shell for which the program has been written since it may use a special function of that shell and not work on others. The sh extension indicates that the standard shell found in /bin/sh is used and is called sh.

As we can see, the character # is used to add comments and the echo instruction to print strings on the screen. Note that although quotes have been used to delimit the string of characters, in that specific case it could have been omitted. echo by default adds a carriage return to the string.

I’M A NEWBIE

The difference between echo "Hello World" and echo Hello World is that in the first case we are passing a single parameter to the echo program while in the second case we are passing two. The echo program prints on the screen all parameters it receives separated by a space. Try adding more than one space between Hello and World in the two previous cases and you will see what happens.

We can also use here-docs and define multi-line comments using here-docs that we don’t pass as a parameter to anything (in fact, we can also do this in Ruby) or using the : operator.

As with all interpreted languages, to run our program we must give it permissions and use the interpreter if we haven’t added the she-bang.

$ bash hello.sh
$ chmod +x hello.sh
$ ./hello.sh

Bash was developed by Brian Fox for the GNU project to replace the Bourne Shell, which was proprietary. Its first version dates back to 1989, and since then it has become the standard shell for many systems. In recent years, other shells (dash, zsh,…) have begun to be used by, especially, some Linux distros, but Bash remains the most widespread.

DID YOU KNOW…

Bash is an acronym for Bourne-again SHell, which is a pun on the name of the shell it’s trying to replace (Bourne Shell) and the idea of being reborn (Born again).

Summary

  • Interpreted language
  • Extension: sh/bash
  • Line terminator: Carriage return
  • Groups instructions with: Depends on the command
  • Multi-line comments: Possible using HERE-DOCS
  • Single-line comments: #
  • String delimiter: "string" or 'string'
  • You can print text with echo (can add (by default) or not carriage return)
  • Supports multi-line strings: Depends on the operation
  • Supports HERE-DOCS
  • Entry point: Beginning of the file

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Ruby

Hello World program in Ruby. Basic structure, comments and string literals

Read

PROGRAMMING RUBY HELLO WORLD

GNU/Linux Programmer's Solver - TCL

Hello World program in TCL. Basic structure, comments and string literals

Read

PROGRAMMING TCL HELLO WORLD

Return to Home Page