Released
7/17/2017Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Printing in the terminal
- Performing math in the Linux shell
- Getting and setting dates
- Working with functions and arguments
- Reading output
- Making comparisons
- Concatenating text
- Finding, editing, generating, and deleting files
- Running parallel processes
- Using regular expressions
- Downloading webpages
- Parsing data from a website
- Finding broken links
- Backing up and archiving
- Transferring files and data through the network
- Monitoring your Linux system
- Gathering data for system administration
Skill Level Intermediate
Duration
Views
- [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.
Related Courses
-
Learning Bash Scripting (2013)
with Scott Simpson1h 25m Beginner -
Linux: Bash Shell and Scripts
with Kevin Dankwardt2h 46m Intermediate
-
1. Shell Something Out
-
Printing in the terminal6m 4s
-
Math with the shell3m 5s
-
Visiting aliases2m 25s
-
Debugging the script2m 46s
-
Functions and arguments4m 31s
-
Comparisons and tests6m 22s
-
-
2. Have a Good Command
-
Concatenating with cat5m 30s
-
Finding files and file listing18m 46s
-
Playing with xargs11m 41s
-
Translating with tr6m 51s
-
Checksum and verification4m 46s
-
Splitting files and data4m 53s
-
Automating interactive input4m 10s
-
-
3. File In, File Out
-
Generating files of any size4m 24s
-
Making files immutable2m 13s
-
Using loopback files6m 15s
-
Printing the directory tree3m 18s
-
-
4. Texting and Driving
-
Using regular expressions9m 25s
-
-
5. Tangled Web? Not at All
-
Downloading from a web page5m 15s
-
A primer on cURL7m 11s
-
Image crawler and downloader3m 36s
-
Web photo album generator1m 54s
-
-
6. The Backup Plan
-
Archiving with tar10m 33s
-
Archiving with cpio2m 31s
-
Compressing data with gzip5m 27s
-
Faster archiving with pbzip23m 15s
-
Backup snapshots with rsync6m 55s
-
-
7. The Old-Boy Network
-
Let us ping4m 53s
-
Creating arbitrary sockets2m 45s
-
-
8. Put on the Monitor's Cap
-
Logging with syslogd2m 44s
-
Monitoring disk activity1m 41s
-
9. Administration Calls
-
Gathering system information1m 57s
-
Scheduling with cron7m 7s
-
User administration script5m 16s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Printing in the terminal