Precautions for writing content to a file with the cat command

The cat command can not only display the content of the file from the standard output to the screen, but also write the content input by the terminal into the file!

However, we will find that sometimes the content written by using the cat command is different from the content we input from the terminal, for example, some strings are specially processed (like) and are not written into the file correctly! This may cause our subsequent instructions to fail to execute normally!

The cat command does not handle special characters: the EOF mark is quoted with single quotes.
If there are variables in the string, the content written to the file will not be changed, such as not replaced with the value of the variable or left blank.
Example:

cat >> /etc/yum.repos.d/base.repo << 'EOF'

[ali-epel]
name=aliyum epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
gpgcheck=0
enabled=1

[mariadb]
name=mariadb
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.2/centos7-amd64/
gpgcheck=0
enabled=1

insert image description here

The cat command handles special characters: EOF does not use single quotes.
If there is a variable in the string, it will be replaced with the value of the variable or left blank
. Example:

cat >> /etc/yum.repos.d/base.repo << EOF

[ali-epel]
name=aliyum epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
gpgcheck=0
enabled=1

[mariadb]
name=mariadb
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.2/centos7-amd64/
gpgcheck=0
enabled=1
EOF

insert image description here

Guess you like

Origin blog.csdn.net/baidu_33864675/article/details/131838180