"cd `" (backtick) command results in ">", why and how do I exit it?

Solution 1:

Backticks work in pairs. Bash is waiting for you to provide another backtick to complete the command/expression.

> is simply a prompt for newline which is determined by the value of PS2 generally defined in .bashrc.

Whenever you hit Enter (if the command/expression is incomplete, i.e. backtick isn't closed), bash expects you to complete the command/expression either in one line or multiple lines. For example, you want to evaluate value of 'a' using expr. You can do

$ a=`
> expr 1 + 3`

will be interpreted as

$ a=`expr 1 + 3`

So, if you want to run some command either complete the required expression or if there is no command/expression required in between backticks, refrain from using that. Another way is to use Ctrl+C, but that would be a Keyboard Interrupt and will make your command to terminate immediately.

To read more about backticks, read these questions on U&L: Understanding backtick and What does ` (backquote/backtick) mean in commands?

Solution 2:

The backticks create an execution environment called command substitution. It is used like this, for example:

echo "The date today is `date`"

Here, the date command is executed first, and its output replaces the part between the backticks, so in the end you get a string with the current date.

Command substitution may span multiple lines, so when you type:

cd `

and press Enter, bash expects you to complete the command substitution, before executing the cd command. This can be broken either by closing the backtick and pressing Enter, or by pressing CTRL-c (This will abort the command without anything getting executed).

Note that modern guidelines prefer to avoid the backtick syntax for command substitution, and to use $( ) instead, so the first example would be:

echo "The date today is $(date)"

Solution 3:

> is a request for more input.

In this case the name of a command is expected to be input. This is because you pressed ` which tells shell that a command name is about to follow.

The easiest way out of this accident is to press Ctrl+C.