简体   繁体   English

Linux 命令和单引号

[英]Linux command and single quote

I thought the single quotes simply reserve the literal value of each character within the quotes and do not interfere with the command result except for those character-escaping situations.我认为单引号只是保留引号内每个字符的字面值,除了那些字符转义情况外,不会干扰命令结果。

For example, the two commands below returned the same result.例如,下面的两个命令返回相同的结果。

$ ls /home/testuser/tmp1
$ ls '/home/testuser/tmp1'

This command below ran successfully.下面这个命令成功运行。

$ cp /home/testuser/tmp1/* /home/testuser/tmp2

However, the command below ran into error.但是,下面的命令出错了。

$ cp '/home/testuser/tmp1/*' /home/testuser/tmp2 
cp: cannot stat '/home/testuser/tmp1/*': No such file or directory

What did I do wrong here?我在这里做错了什么?

* is what you call a character escaping situation. *就是你所说的字符转义情况。 Since you preserved the literal value of * , it lost its meaning of "all files", and cp instead tries to copy a file named literally * .由于您保留了*的字面值,因此它失去了“所有文件”的含义,而cp而是尝试复制字面命名的文件*

When you run:当你运行时:

cp /home/testuser/tmp1/* /home/testuser/tmp2 

The shell will transparently rewrite it into: shell 将透明地将其重写为:

cp /home/testuser/tmp1/bar /home/testuser/tmp1/foo /home/testuser/tmp2 

This process is known as "pathname expansion" or "globbing".此过程称为“路径名扩展”或“通配”。 When you quote the * , this rewrite does not happen.当您引用* ,不会发生这种重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM