简体   繁体   English

bash命令行中用双引号引起来的字符串cmd

[英]The string cmd enclosed by double quotes in bash commandline

I am a beginner of bash. 我是bash的初学者。 I encounter a problem like this: 我遇到这样的问题:

   $ "make -p"  

when I type the above in bash command line, there is nothing to happen, no error, no result msg. 当我在bash命令行中输入以上内容时,没有任何反应,没有错误,没有结果msg。

I have searched double quotes syntax of bash in many websites. 我在许多网站中搜索了bash的双引号语法。 All of these materials give similar interpretation as below: 所有这些材料给出了类似的解释,如下所示:
https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
and give examples like: 并给出如下示例:

echo "argument"  

I do not find something like "echo argument" . 我找不到类似"echo argument"东西。 Moreover, I find a strange difference between bash command line and bash scripts. 而且,我发现bash命令行和bash脚本之间有一个奇怪的区别。

If I type a non-existing command in command line: 如果我在命令行中键入了不存在的命令:

$ "holy shit"
$ "look that"

there is nothing to happen. 什么也没发生 But if I type it in bash scripts: 但是,如果我在bash脚本中键入它:

#!/bin/bash  
"holy shit"  
"look that"  

and execute this script, an error msg will be throw out: 并执行以下脚本,将抛出错误消息:

$ ./myshell   
./myshell: line 2: holy shit: command not found  
./myshell: line 3: look that: command not found

Would someone can help give a detailed interpretation about the effect of double quotes when they enclosed the whole command? 当双引号将整个命令括起来时,是否可以帮助详细解释双引号的作用?
Why there is no output in command-line? 为什么命令行中没有输出? Why it is different between command line and scripts? 为什么命令行和脚本之间有区别?

The double quotes mean it is a string. 双引号表示它是一个字符串。 You can do something like: 您可以执行以下操作:

echo "Hello everybody"

either at the command line or the shell. 在命令行或shell中。 Sometimes when people put stuff in quotes. 有时当人们用引号引起来 you are supposed to replace what is in quotes with your own variable (removing the quotes), and sometimes people put quotes around the whole command you are supposed to type to show the what exactly you should type. 您应该用自己的变量替换引号(删除引号),有时人们会在您应该键入的整个命令周围加上引号,以显示您应该键入的内容。 For your example of "make -p" just type it without the quotes and it should work in both the command line and as a script. 对于您的“ make -p”示例,只需键入不带引号的名称,即可在命令行和脚本中使用。

If you enter a command foo , the shell searches the directories listed in your PATH variable until it finds a command of this name. 如果输入命令foo ,则外壳程序将搜索PATH变量中列出的目录,直到找到具有该名称的命令为止。 If there is none, you get the error message command not found . 如果不存在,则会显示未找到错误消息命令

If you enter a command, which contains at least one slash - for example ./foo or foo/bar -, the shell does not search the PATH, but assumes that you have already entered the correct path to your command. 如果你输入一个命令,它至少包含一个斜杠-例如./foofoo/bar - ,外壳不会搜索路径,但假定你已经输入了正确的路径,你的命令。 If it does not exist, you get the error message No such file or directory . 如果不存在,则会出现错误消息No such file or directory

In your case, 就你而言

 "cd home"

searches for a file with name cd home somewhere along your PATH , but there is no file of this name, and you get command not found . 在您的PATH某处搜索名称为cd home的文件,但是没有该名称的文件,并且您找不到命令 If you enter 如果输入

"cd /home"

the shell bypasses PATH-search and assumes, that there exists a directory named cd (ie the 3 letters c,d,space) in your current directory, and below it a file named home, with x-bit set. Shell绕过PATH搜索,并假定当前目录中存在一个名为cd的目录(即3个字母c,d,space),在其下是一个名为xhome的文件,并设置了x位。 There is no such file (and no such directory) on your system, and you get the error message No such file or directory . 您的系统上没有这样的文件(也没有这样的目录),并且您收到错误消息No such file or directory

If you are in the mood of experimenting around, you could try the following: 如果您想尝试一下,可以尝试以下方法:

mydir="cd "
mkdir "$mydir"
echo "echo Hello Stranger" >"$mydir/home"
chmod +x "$mydir/home"
"cd /home"

This should print Hello Stranger . 这应该打印Hello Stranger Pay attention that in the assignment to mydir , there must be a single space between the cd and the closing quote. 请注意,在分配给mydir的过程中cd和右引号之间必须有一个空格。

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

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