1

I accidentally typed in cd ` into terminal today and terminal didstrange things.

It put a "> " signed on the next line followed by my cursor like it wanted some input. No matter what I entered continued to do the same thing until I terminated the command.

Out of curiosity what happened? Was this a bug or a feature?

yesennes
  • 173

2 Answers2

4

The effect you witnessed is due to the shell expecting another backtick. You get the same effect when you, for example, enter ( (or any other incomplete command) and press Return on the command line.

The prompt, >␣, that you got is the secondary prompt, as stored in $PS2. This prompt is used by the shell when more interactive input is needed to finally complete the command.

An example:

bash-4.4$ for i in {0..10..2}
> do
> printf 'Hello %d\n' "$i"
> done
Hello 0
Hello 2
Hello 4
Hello 6
Hello 8
Hello 10
bash-4.4$

bash-4.4$ is my primary prompt ($PS1), and I get the secondary prompt when pressing Return after {0..10..2} on the first line since the command typed in so far is not complete. I continue to get the secondary prompt on each line until I have completed the command by pressing Return after done (which completes the input of the for-loop to the shell).

The tertiary prompt ($PS3, usually #?␣) is used by the select keyword, while the quaternary1 prompt ($PS4, usually +␣) is used for tracing output when the shell xtrace option is set with set -x.


1 Yes, I had to look up that word.

Kusalananda
  • 333,661
1

Answered here already... essentially

Everything you type between backticks is evaluated (executed) by the shell before the main command

Migs
  • 894
  • 1
    I had read that and tried typing commands to no effect. Your post made me think to try another backtick, which acted as in the other post. – yesennes Sep 30 '15 at 03:49