- [Narrator] Shell something out.
The primary purpose of this section
is to give readers an insight into the shell environment
and become familiar with the basic features
that the shell offers.
Commands are typed and executed in a shell terminal.
Now, we will see the first video
of Section One, Printing in the Terminal.
The Terminal is an interactive utility
by which a user interacts with a shell environment.
Printing text in a terminal is a basic task
that most shell scripts and utilities need
to perform regularly.
As we'll see in this video, this can be performed
via various methods and in different formats.
Now, we'll see how to do it.
Echo is the basic command for printing in the terminal.
Echo puts a new line at the end of every Echo invocation,
by default.
Simply using double-quoted text with the Echo command
prints the text in the terminal.
Similarly, text without double-quotes also
gives the same output.
Another way to do the same task is by using single quotes.
These methods may look similar but some of them
have a specific purposes and side effects too.
Consider this command.
This will return the output as "event not found" error.
Hence, if you want to print special characters
such as the exclamation mark,
either do not use them with double-quotes
or escape them with a special character, "slash",
prefixed with it like so.
The side effects of each of the methods are as follows:
When using Echo without quotes,
we cannot use a semicolon as it acts as an delimiter
between commands in the shell.
Echo hello, semicolon hello takes Echo hello
as one command.
And the second hello as a second command.
Variable substitution which is discussed
in the next section will not work within single quotes.
Another command for printing in the terminal
is Printf.
It uses the same arguments as the Printf command
in the C programming language.
For example.
Printf takes quoted text or arguments delimited
by spaces.
We can use formatted strings with Printf.
We can specify string width, left or right alignment,
and so on.
By default, Printf does not have New Line
as in the Echo command.
We have to specify a new line when required
as shown on the screen.
We will receive the formatted output.
Let's see how it works.
%s, %c, %d, and %f are format substitution characters
for which an argument can be placed after
the quoted format string.
"%-5s" can be described as a string substitution
with left alignment.
Dash represent left alignment.
With width equal to five.
If dash was not specified, the string would been aligned
to the right.
The width specifies a number of characters
reserved for that variable.
For name, the width reserve is 10,
hence, any name will reside within
the 10-character width reserve for it
and the rest of the characters will be filled up
with a space up to 10 characters in total.
For floating point numbers, we can pass
additional parameters to round off decimal places.
For marks, we have formatted the string as "%-4.2f"
where dot-two specifies rounding off to two decimal places.
Note that for every line of the format string,
a new line, \n, is issued.
Now, we can run the command by using bash
followed by the path name and the file name.
While using flags for Echo and Printf,
always make sure the flags appear before any strings
in the command.
Otherwise, bash will consider the flags
as another string.
Escaping New Line and Echo.
By default, Echo has a new line appended
at the end as output text.
This can be avoided by using the "-n" flag.
Echo can also accept escape sequences
in double-quoted strings as an argument.
When using escape sequences,
use Echo as "Echo -e", quotes,
string-containing-escape-sequences, close quotes,
for example.
Printing a colored output.
Producing a colored output on a terminal
is very interesting and is achieved using escape sequences.
Colors are represented by color codes.
Some examples being Reset equals 0.
Black equals 30.
Red, 31.
Green, 32.
Yellow, 33.
Blue, 34.
Magenta, 35.
Cyan, 36.
And white, 37.
Let's see how to print colors.
To print a colored text,
enter the command which is shown here.
Here is the command which would be in a red color.
Here, "\e[1;31n" is the escape string
that sets the color to red.
And, "\e[0n" resets the color back.
Replace 31 with the required color code.
For a colored background, reset equals 0.
Black equals 40.
Red, 41.
Green, 42.
Yellow, 43.
Blue, 44.
Magenta, 45.
Cyan, 46.
And white, 47.
The color codes that are commonly used.
To print a colored background,
enter the command which is shown here.
Nice.
We've learned about printing in the terminal.
In the next one, we'll learn how to play with variables
and environment variable.