什么是'和'在bash有什么区别? [英] What's the difference between ` and ' in bash?

查看:125
本文介绍了什么是'和'在bash有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS X终端运行此语句

 对我`LS -v * .mkv`;做回声$ I; DONE

将成功地打印出目录中的所有文件名中的名称顺序与自身线上的每个文件名。

来源:这StackOverflow的答案

不过,如果我在OS X终端运行该语句

 因为我在'LS -v *的.mkv';做回声$ I; DONE

的输出为LS -v fileName1.mkv fileName2.mkv等具有串连成一长行(相对于每一个正在打印在单独的行)中的所有文件名称。

我的问题是:


  1. 有什么和在bash'之间的区别?

  2. 为什么负责完全不同的输出区别?

  3. 什么键盘组合产生`? (键盘组合)


解决方案

1)之间的文本反引号执行和封闭的命令,所以输出代替:

 回声`回声42`

将扩展到:

  42回声

这是称为命令替换并还可以使用所获得的语法 $(命令)。你的情况,如下:

对我`LS -v * .mkv`;做...

是由类似取代(如果您的目录包含3个文件名为 a.mkv b.mkv 的和的 c.mkv 的):

对我a.mkv b.mkv c.mkv;做...

行情双引号只是普通击弦像符之间的文本<大骨节病>空格在他们里面scaped(还有其他的方式引用字符串在Bash和这里)描述

回声这仅仅是一个简单明了字符串
呼应这是另一个字符串

使用之间的差异 之间封闭的字符串可以插值变量,例如:

变量= 42
回声你的值是$变量

或者

 变量= 42
回声你的值是$ {}变量

打印:

您值为42


2)通配符前pressions像 *。MKV 在被称为的通配符。通配的使用通配符激活大多数命令的包围在字符串中的前pression:

 回声*的.mkv

将会打印:

  a.mkv b.mkv c.mkv

同时

 回声*的.mkv

打印:

  *。MKV

循环中的 I 变量取值 LS -v *的.mkv ,但循环体中的回声命令将 $我不带引号,所以猛砸施加通配符那里,你结束了以下内容:

 因为我在'LS -v *的.mkv';做
   #$回声我
   #
   #其扩展为:
   #回声LS -v *的.mkv(不含引号)
   #
   #和文件名匹配的过程转变到上述情况:
   回声LS -v a.mkv b.mkv c.mkv

这仅仅是一个单行字符串应用于文件名匹配后的文件名。


3)这取决于你的键盘布局。

一招让角色就是使用该程序 ASCII ,搜索字符96(十六进制60),将其复制并保持它在你的剪贴板(您可以使用 parcellite 或适合您需要的任何其他剪贴板经理)。


更新:的建议通过 @triplee ,你应该检查的没用使用 LS ,因为这被认为是的庆典陷阱,并有更好的方法来实现你重新尝试做的。

Running this statement in OS X Terminal

for i in `ls -v *.mkv`; do echo $i; done

will successfully print out all the file names in the directory in name order with each file name on its own line.

Source: This StackOverFlow answer

However, if I run this statement in OS X Terminal

for i in 'ls -v *.mkv'; do echo $i; done

the output is "ls -v fileName1.mkv fileName2.mkv", etc. with all the file names concatenated into one long line (as opposed to each being printed on its own line).

My questions are:

  1. What's the difference between ` and ' in bash?
  2. Why is that difference responsible for the completely different output?
  3. What keyboard combination produces `? (Keyboard combination)

解决方案

1) Text between backticks is executed and replaced by the output of the enclosed command, so:

echo `echo 42`

Will expand to:

echo 42

This is called Command Substitution and can also be achieved using the syntax $(command). In your case, the following:

for i in `ls -v *.mkv`; do ...

Is replaced by something like (if your directory contains 3 files named a.mkv, b.mkv and c.mkv):

for i in a.mkv  b.mkv  c.mkv; do ...

Text between quotes or double quotes are just plain Bash strings with characters like space scaped inside them (there are other ways to quote strings in Bash and are described here):

echo "This is just a plain and simple String" 
echo 'and this is another string' 

A difference between using ' and " is that strings enclosed between " can interpolate variables, for example:

variable=42
echo "Your value is $variable"

Or:

variable=42
echo "Your value is ${variable}"

Prints:

Your value is 42


2) Wildcard expressions like *.mkv are replaced by the expanded filenames in a process known as Globbing. Globbing is activated using wildcards in most of the commands without enclosing the expression inside a string:

echo *.mkv

Will print:

a.mkv b.mkv c.mkv

Meanwhile:

echo "*.mkv"

prints:

*.mkv

The i variable in your for loop takes the value "ls -v *.mkv" but the echo command inside the loop body takes $i without quotes, so Bash applied globbing there, you end up with the following:

for i in 'ls -v *.mkv'; do 
   #   echo $i 
   #
   # which is expanded to:
   #   echo ls -v *.mkv    (no quotes) 
   #
   # and the globbing process transform the above into:
   echo ls -v a.mkv b.mkv c.mkv

Which is just a one-line string with the file names after the globbing is applied.


3) It depends on your keyboard layout.

One trick to keep the character around is to use the program ascii, search for the character 96 (Hex 60), copy it and keep it on your clipboard (you can use parcellite or any other clipboard manager that suits your needs).


Update: As suggested by @triplee, you should check useless use of ls as this is considered a bash pitfall and there are better ways to achieve what you're trying to do.

这篇关于什么是'和'在bash有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆