11

When I enter single quote ' in terminal it goes to some other mode, and commands don't execute. What is this mode and when do we use it?

root@sai:~# '
> ls
> '
ls
: command not found
root@sai:~#
  • 29
    and why are you running as root? – Zanna Mar 8 '17 at 7:15
  • 13
    Your terminal is in the same mode as before - your shell is in a different mode (waiting for the completion of a string with another ') – ohno Mar 8 '17 at 13:30
26

Effectively, the shell asks for a complete command/expression, and for that reason is displaying the PS2 prompt string.

From man bash:

PROMPTING

When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command.

And a little before that:

  PS2    The value of this parameter is  expanded
          as  with  PS1  and used as the secondary
          prompt string.  The default is ``> ''.

Thus, as you may guess from reading the documentation, shells have multiple prompts with different purposes. The PS1 prompt is your root@sai:~# string, which shows up normally when you enter commands. > is the PS2 prompt. There's others, too: PS3 for select command block and PS4 for debugging with set -x command. In this case we're more interested in PS2.

There are many ways in which shell may show the PS2 prompt (and where completing a command on a new line might be necessary). The same prompt is used when you perform here-doc redirection (where a command is considered complete when you see the terminating string, in this example, EOF):

$ cat <<EOF
> line one
> line two
> EOF
line one
line two

Very often continuation of a lengthy command can be done by adding \ and immediate(!) newline, which will cause the same prompt to appear:

$ echo Hello\
> World
HelloWorld

$ echo 'Hello\                                                                                                           
> World'
Hello\
World

When pipes, logic operators, or special keywords appear on command-line before newline, the command also is considered incomplete until all final statements are entered:

$ echo Hello World | 
> wc -l
1

$ echo Hello World &&                                                                                                    
> echo "!"
Hello World
!

$ for i in $(seq 1 3); do
> echo "$i"
> done
1
2
3 

$ if [ -f /etc/passwd ]
> then
>     echo "YES"
> fi
YES

In your particular case, a single quote implies literal interpretation of what is between the single quotes. Thus, as Zanna pointed out, you are entering a command that consists of newline+ls+newline. Such an executable filename cannot be found (and typically command filenames should consist of only alphanumeric characters, plus underscores, dashes, and dots). Although it is indeed possible to have filenames that contain special characters in them, it is always avoided.

NOTE: such behavior as shown in your example is specific to Bourne-like shells, including bash, dash (on Ubuntu it is symlinked to /bin/sh), ksh, and mksh. csh and its derivatives do not behave in such way:

$ tcsh                                                    
eagle:~> '
Unmatched '.
eagle:~> csh
% '
Unmatched '.
%  

However, in interactive mode, csh will still raise ? as prompt2 when more input is required:

$ csh
% foreach n ( 1 2 3 )
? echo $n
? end
1
2
3  

See also:

  • The link What's the difference between <<, <<< and < < in bash? is offline/wrong. – Tico Mar 8 '17 at 16:49
  • @Tico Thanks fixed. The answer was written with internet speed of milli-turtles per second, which resulted only in partially copied link. Fixed now – Sergiy Kolodyazhnyy Mar 8 '17 at 16:51
  • 3
    Meanwhile, zsh is kind enough to actually tell you what it is waiting for, which is occasionally useful if you thought your command was valid but forgot to escape something. – Kevin Mar 9 '17 at 8:02
30

The shell is just waiting for the closing quote. When you enter it, it will do exactly what it usually does, and attempt to execute the command entered.

Quotes cause the shell not to interpret special characters, meaning that expansions are not performed. Single quotes suppress all interpretation of special characters completely. Normally a newline separates commands, but here you have included the newlines as part of the command by quoting them.

Since there is no such command as <newline>ls<newline>, it is not found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.