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

How to implement responsiveness in Vue source code learning

Table of contents Preface 1. Key Elements of a Re...

How to automatically deploy Linux system using PXE

Table of contents Background Configuring DHCP Edi...

Angular Dependency Injection Explained

Table of contents Overview 1. Dependency Injectio...

Vue+element implements drop-down menu with local search function example

need: The backend returns an array object, which ...

How to set up remote access to a server by specifying an IP address in Windows

We have many servers that are often interfered wi...

React Hooks Detailed Explanation

Table of contents What are hooks? Class Component...

CocosCreator ScrollView optimization series: frame loading

Table of contents 1. Introduction 2. Analysis of ...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

Native js canvas to achieve a simple snake

This article shares the specific code of js canva...

A mobile adaptive web page effect solves the problem of small display page

For work needs, I need to make a mobile phone adap...

MySQL 8.0.18 installation tutorial under Windows (illustration)

Download Download address: https://dev.mysql.com/...

Detailed explanation of some settings for Table adaptation and overflow

1. Two properties of table reset: ①border-collaps...

Solve the problem that Docker cannot ping the host machine under Mac

Solution Abandon the Linux virtual machine that c...

JavaScript implements the nine-grid click color change effect

This article shares the specific code of JavaScri...

Detailed tutorial on installing MySQL 8.0.19 in zip version on win10

Table of contents 1. After downloading, unzip it ...