A Deep Understanding of Angle Brackets in Bash (For Beginners)

A Deep Understanding of Angle Brackets in Bash (For Beginners)

Preface

Bash has many important built-in commands such as ls, cd, and mv, as well as many useful tools such as grep, awk, and sed. But in addition to these, there are actually many punctuation marks in Bash that can act as glue, such as the period (.), comma (,), brackets (<>), quotation marks ("), etc. Next, let's take a look at the angle brackets (<>) that can be used for data conversion and transfer.

Transferring Data

If you know anything about other programming languages, you will know that angle brackets < and > are generally used as logical operators to compare the size relationship between two values. If you still write HTML, angle brackets will be familiar to you as part of various tags.

In shell scripting, angle brackets can be used to transfer data from one place to another. For example, you can store data in a file like this:

ls > dir_content.txt

In the above example, the > symbol tells the shell to write the output of the ls command to dir_content.txt instead of displaying it directly on the command line. Note that if the dir_content.txt file does not exist, Bash will create it for you; however, if dir_content.txt is an existing non-empty file, its contents will be overwritten. So be careful before performing similar operations.

You can also use >> instead of > to append new data to the end of the file without overwriting existing data. For example:

ls $HOME > dir_content.txt; wc -l dir_content.txt >> dir_content.txt

In this series of commands, the contents of the home directory are first written to the dir_content.txt file, and then the number of lines in the dir_content.txt file (that is, the number of files in the home directory) is calculated using wc -l and appended to the end of dir_content.txt.

After executing the above command on my machine, the content of dir_content.txt will be as follows:

Applications
bin
cloud
Desktop
Documents
Downloads
Games
ISOs
lib
logs
Music
OpenSCAD
Pictures
Public
Templates
test_dir
Videos
17 dir_content.txt

You can think of > and >> as arrows. Of course, the direction of this arrow can also be reversed. For example, some actors of the Coen brothers and the number of times they appeared in the movies are stored in the CBActors file, like this:

John Goodman 5
John Turturro 3
George Clooney 2
Frances McDormand 6
Steve Buscemi 5
Jon Polito 4
Tony Shalhoub 3
James Gandolfini 1

You can execute the command like this:

sort < CBActors
Frances McDormand 6 # You will get output like this George Clooney 2
James Gandolfini 1
John Goodman 5
John Turturro 3
Jon Polito 4
Steve Buscemi 5
Tony Shalhoub 3

You can use the sort command to output this list in alphabetical order. However, the sort command can already accept a file as an argument, so using < here would be redundant, and directly executing sort CBActors would yield the desired result.

If you want to know who the Coens' favorite actor is, here's how you can do it. first:

while read name surname films; do echo $films $name $surname > filmsfirst.txt; done < CBActors

The above sequence of commands might be easier to read if written on multiple lines:

while read name surname films;\
do
echo $films $name $surname >> filmsfirst;\
done < CBActors

Let's analyze what these commands do:

  • while …; do … done is a loop structure. When the condition after while is true, the part between do and done will be repeatedly executed;
  • The read statement reads the content line by line. read will continue to read from standard input until there is nothing left to read;
  • The contents of the CBActors file will be read from standard input via <, so the while loop will read the CBActors file completely line by line;
  • The read command can divide each line into three fields according to spaces, and then assign the three fields to the name, surname, and films variables respectively. In this way, you can easily rearrange the order of the fields and store them in the filmsfirst file through echo $films $name $surname >> filmsfirst;\.

After the execution is complete, check the filmsfirst file and the content will be as follows:

5 John Goodman
3 John Turturro
2 George Clooney
6 Frances McDormand
5. Steve Buscemi
4 Jon Polito
3 Tony Shalhoub
1 James Gandolfini

Now use the sort command:

sort -r filmsfirst

You can see that the Coens' favorite actor is Frances McDormand. (-r parameter means descending order, so McDormand will be at the top)

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • More Ways to Use Angle Brackets in Bash
  • Using brackets and backticks in Bash scripts

<<:  2017 latest version of windows installation mysql tutorial

>>:  JavaScript to dynamically load and delete tables

Recommend

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

CentOS7 upgrade kernel kernel5.0 version

Upgrade process: Original system: CentOS7.3 [root...

Vue scroll down to load more data scroll case detailed explanation

vue-infinite-scroll Install npm install vue-infin...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

Implementation of Single Div drawing techniques in CSS

You can often see articles about CSS drawing, suc...

Ubuntu 20.04 how to modify the IP address example

illustrate: Today, when continuing the last offic...

Vue uses custom instructions to add watermarks to the bottom of the page

Project Scenario Add a custom watermark to the en...

Solution to EF (Entity Framework) inserting or updating data errors

Error message: Store update, insert, or delete st...

How to check the hard disk size and mount the hard disk in Linux

There are two types of hard disks in Linux: mount...

9 Practical CSS Properties Web Front-end Developers Must Know

1. Rounded Corners Today's web designs are con...

Nginx forwarding based on URL parameters

Use scenarios: The jump path needs to be dynamica...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

Mysql aggregate function nested use operation

Purpose: Nested use of MySQL aggregate functions ...

Detailed explanation of Vue's list rendering

Table of contents 1. v-for: traverse array conten...