Hello World in shell
Whenever we learn a new programming language, we first learn how to write the Hello World program in it. It is the best way to know and interact with a new language. This also helps in confirming that the basic environment for a program in a given language has been set up and you are good to dive deep inside this language.
Interacting with shell
We can print the output of commands in console in an interactive way. Console is also known as a standard input and output stream. To print anything in a bash
console, use the echo
command followed by what is to be printed:
$ echo Hello World Hello World
Alternatively, put the text to be printed in double quotes:
$ echo "Hello World" Hello World
You can also put the text to be printed in single quotes:
$ echo 'Hello World' Hello World
We can also use the printf
command in shell programming for printing. The printf
command also supports formatted printing, similar to what we have in C programming language— the printf( )
function:
$ printf "Hello World" Hello World$
Here, after the output, we see the command prompt ($
) because printf
doesn't add a default newline after execution while echo does. So, we have to explicitly add the newline (\n
) in the printf
statement to add a newline:
$ printf "Hello World\n" Hello World
Similar to the C printf( )
, we can specify formatted printing in bash
. The syntax of bash
printf
is as follows:
printf FORMAT [ARGUMENTS]
FORMAT
is a string that describes the format specifications and is specified within double quotes. ARGUMENTS
can be the value or a variable corresponding to format specification. Format specification consists of the percentage (%
) sign followed by format specifier. Format specifiers are explained in the following table:
Format specification |
Description |
---|---|
|
This prints an unsigned integer value |
|
This prints an associated argument as a signed number |
|
This prints an associated argument as a floating point number |
|
This prints an unsigned octal value |
|
This prints a string value |
|
This prints an unsigned hexadecimal value (0 to 9 and A to F) |
|
This prints an unsigned hexadecimal value (0 to 9 and a to f) |
The following examples demonstrate how to use format specification for printing different data type format in shell:
$ printf "%d mul %f = %f\n" 6 6.0 36.0 6 mul 6.000000 = 36.000000 $ printf "%s Scripting\n" Shell Shell Scripting
We can also optionally specify a modifier in format specification to align an output to provide better formatting to the output. Format modifiers are placed between %
and the format specifier character. The following table explains format modifiers:
Format Modifiers |
Description |
---|---|
N |
This is any number that specifies a minimum field width. |
. |
This is used together with field width. The field doesn't expand when the text is longer. |
- |
This is the left-bound text printing in the field. |
0 |
This is used to fill padding with zeros (0) instead of whitespaces. By default, padding is done with whitespaces. |
The following example demonstrates how to use format modifiers to improve printing formatting:
$ printf "%d mul %.2f = %.2f\n" 6 6.0 36.0 6 mul 6.00 = 36.00
Let's make it scripted
Interactive printing is good if we have to print one or two lines, but for a lot of printing, it's good and preferred to write a script file. A script file will contain all the instructions and we can run a script file to perform the needed task.
Now, we are going to create a bash
script file that makes use of the echo
and printf
commands and print messages:
#!/bin/bash #Filename: print.sh #Description: print and echo echo "Basic mathematics" printf "%-7d %-7s %-7.2f =\t%-7.2f\n" 23 plus 5.5 28.5 printf "%-7.2f %-7s %-7d =\t%-7.2f\n" 50.50 minus 20 30.50 printf "%-7d %-7s %-7d =\t%-7d\n" 10 mul 5 50 printf "%-7d %-7s %-7d =\t%-7.2f\n" 27 div 4 6.75
The first line in bash
script represents the path of the interpreter used. The second line is a comment line telling the filename of a script file. In shell script, we use #
to add a comment. Furthermore, the echo
command will print strings written within double quotes. For the rest, we have used printf
to print formatted output.
To run this script, we will first provide execute permission to a user/owner of this script:
$ chmod u+x print.sh
Then, run the script file in console as follows:
$ ./print.sh
The result after running this script will look as follows: