简体   繁体   中英

The string cmd enclosed by double quotes in bash commandline

I am a beginner of 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.

I have searched double quotes syntax of bash in many websites. All of these materials give similar interpretation as below:
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" . Moreover, I find a strange difference between bash command line and bash scripts.

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:

#!/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. 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.

If you enter a command foo , the shell searches the directories listed in your PATH variable until it finds a command of this name. 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. If it does not exist, you get the error message 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 . 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. There is no such file (and no such directory) on your system, and you get the error message 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 . Pay attention that in the assignment to mydir , there must be a single space between the cd and the closing quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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