bash 单引号 双引号_Bash Shell中的单引号和双引号有什么区别?

bash 单引号 双引号

bash 单引号 双引号

Enclosing text in quotation marks is fairly standard practice on the command line, especially when dealing with files that have spaces in the names, but how do you know whether to use single or double quotes? Let’s take a look at the difference, and when you should use one vs the other.

在命令行上将文本括在引号中是相当标准的做法,尤其是在处理名称中带有空格的文件时,但是如何知道使用单引号还是双引号呢? 让我们看一下两者之间的区别,以及何时应该使用一个与另一个。

The general rule is that double quotes still allow expansion of variables within the quotes, and single quotes don’t. Keep reading.

一般规则是,双引号仍然允许在引号内扩展变量,而单引号则不允许。 继续阅读。

简单文字引号 (Quotes with Simple Text)

If you’re simply enclosing a few words of text, it really doesn’t matter which one you use, as they will both work exactly the same. For instance, these two commands will both create a directory named Test Directory:

如果只封装几个文本,那么使用哪个文本就没有关系,因为它们的工作原理完全相同。 例如,这两个命令都将创建一个名为Test Directory的目录:

mkdir "Test Directory"
mkdir 'Test Directory'

mkdir "Test Directory"
mkdir 'Test Directory'

More experienced types will probably note that you could also use mkdir Test\ Directory if you wanted.

比较有经验的类型可能会注意到,如果需要,您也可以使用mkdir Test \ Directory。

Shell变量展开 (Shell Variable Expansion)

The difference between single and double quotes becomes more important when you’re dealing with variables on the command line—by default, bash is going to expand a variable like $test into the full string. First, we’ll assign the variable:

当您在命令行上处理变量时,单引号和双引号之间的区别变得更加重要-默认情况下,bash会将$ test之类的变量扩展为完整字符串。 首先,我们将分配变量:

test="This is a test"

test="This is a test"

Now you can use this variable on the command line, like this, which should simply output This is a test to the console:

现在,您可以像这样在命令行上使用此变量,该变量应简单地输出这是对控制台的测试

echo $test

echo $test

Here’s where the difference between double and single quotes becomes more clear—when you use single quotes the variables aren’t going to be expanded. For instance, if you run this command:

这是双引号和单引号之间的区别更加明显的地方-当您使用单引号时,变量将不会扩展。 例如,如果您运行以下命令:

echo '$test'

echo '$test'

You’ll see nothing but ‘$test’ on the command line when you use single quotes, but it will output properly when you use double quotes:

使用单引号时,您只会在命令行上看到“ $ test”,但使用双引号时,它将正确输出:

image

The same thing works when you use the ` character to expand a command on the command line. For instance, say you wanted to use the pwd command from within another command—you’d use backticks to tell the shell to expand it:

当您使用`字符在命令行上展开命令时,同样的事情也起作用。 例如,假设您要在另一个命令中使用pwd命令-您将使用反引号告诉shell对其进行扩展:

echo `pwd`/test

echo `pwd`/test

If you were in your home folder you’d see output that looked like this:

如果您位于主文件夹中,则会看到如下所示的输出:

/home/geek/test

/ home / geek / test

Say, for example, you are in a folder that has a space in the path, and you want to use the ln command to create a symbolic link to a file in the current directory. You usually need to specify the full path when using the ln command, so it’s much easier to use `pwd` to save typing.

举例来说,假设您位于路径中有空格的文件夹中,并且想要使用ln命令来创建指向当前目录中文件的符号链接。 通常在使用ln命令时需要指定完整路径,因此使用`pwd`保存键入要容易得多。

Look what happens when you try and use the command without enclosing in quotes:

看看尝试使用命令而不用引号引起来的情况:

ln –s `pwd`/test /home/geek/linkedname

ln –s `pwd`/test /home/geek/linkedname

image

Instead, you’ll need to surround in quotes:

相反,您需要将引号引起来:

ln –s "`pwd`/filename" /some/other/path

ln –s "`pwd`/filename" /some/other/path

For a more concrete example, let’s assume that we have a folder structure like this example, where all the file names have spaces in them:

对于一个更具体的示例,让我们假设我们有一个类似于此示例的文件夹结构,其中所有文件名中都带有空格:

image

Since the unzip command doesn’t support using * to run it against all files, you’ll need to use the for command instead. Here’s where things get interesting:

由于unzip命令不支持使用*对所有文件运行它,因此需要使用for命令。 这是有趣的地方:

for f in *.zip;do unzip $f;done

for f in *.zip;do unzip $f;done

Oops! Looks like it didn’t work.

糟糕! 看起来没用。

image

What we’ll need to do instead is use double quotes around the $f variable, like this:

我们需要做的是在$ f变量周围使用双引号,如下所示:

for f in *.zip;do unzip "$f";done

for f in *.zip;do unzip "$f";done

Now every time the for command goes through the loop, it’s going to actually run a command like this one:

现在,每次for命令遍历循环时,它将实际上运行如下命令:

unzip "test 1.zip"

unzip "test 1.zip"

Of course it would loop through and run a similar command for every file in the directory.

当然,它将遍历目录并为目录中的每个文件运行类似的命令。

让我们来复习 (Let’s Review)

Now that we’ve gone through the examples, we’ll just quickly review in case you missed it:

现在,我们已经遍历了示例,我们将快速回顾一下,以防您错过它:

Double Quotes

双引号

  • Use when you want to enclose variables or use shell expansion inside a string.

    当您要封装变量或在字符串内使用shell扩展时使用。
  • All characters within are interpreted as regular characters except for $ or ` which will be expanded on the shell.

    其中的所有字符都将解释为常规字符,但$或`会在外壳程序上扩展。

Single Quotes

单引号

  • All characters within single quotes are interpreted as a string character.

    单引号内的所有字符均被解释为字符串字符。

And thus ends the lesson of the quotes. Use them wisely.

从而结束了引文的课程。 明智地使用它们。

翻译自: https://www.howtogeek.com/howto/29980/whats-the-difference-between-single-and-double-quotes-in-the-bash-shell/

bash 单引号 双引号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值