What is the purpose of cd ` (backtick)?

56

9

The following behaves the same in Fedora and CentOS.

cd mydirectory

works.

cd notadirectory

works (gets an error from Bash).

However,

cd `

gives me some sort of prompt:

>

Nothing I've thought to enter gives any response, it merely heads to the next line to prompt again. I simply use CTRL+C to exit back to my root@localhost.

What is the purpose of this and what can one do with it?

Thanks!

Mooseman

Posted 2017-04-25T22:03:08.497

Reputation: 1 232

43

See What does ` (backquote/backtick) mean in commands?

– wjandrea – 2017-04-26T05:31:35.127

6So your question is more about the > than about the backtick, since that can also occur in other situations – Tobias Kienzler – 2017-04-26T06:04:13.553

Answers

123

What you've typed is a backtick - it is the start of an instruction to bash to evaluate what you type as a command. The > is displayed to indicate you are still entering the command on the next line.

If you close the backtick you'll find the whole command will run. E.g.

~$ cd `
> echo /var`
/var$

John C

Posted 2017-04-25T22:03:08.497

Reputation: 1 034

17You can even nest them. For a somewhat realistic example: cd \dirname \`which ls\``` = cd to the directory where the "ls" binary is located. (i.e. cd /bin since it's /bin/ls) [edit: phew, escaping that markdown is HARD] – MSalters – 2017-04-26T08:19:28.663

54@MSalters You've perhaps stumbled upon the reason why modern bash prefers $() to \``. Your example (plus the correct quoting) would become cd "$(dirname "$(which ls)")" which is much less insane. – Muzer – 2017-04-26T09:20:41.173

31

@Muzer It's not just bash that prefers it, but it's recommended in the POSIX specification to use $() over backquotes.

– Starfish – 2017-04-26T17:24:33.053

59

JohnC's answer already explains the backtick. But what you are also wondering about is the > prompt. This a continuation prompt, and it is not only triggered by a backtick, but always when your shell clearly knows you're not done entering a command. The easiest example is putting an explicit line continuation \ at the end of an input line (which helps splitting long input):

$ echo \
> hallo

Note that just like PS1 controls the command prompt's look, you can also set PS2 to change the continuation prompt, e.g.

$ export PS2="(cont.) "
$ echo \
(cont.) hallo

There are many reasons for the continuation to occur. A single backtick is incomplete, but you could also enter something like

ls -l `which cp`

in a single line (side-note: It's recommended to use $( and ) instead, since the parentheses make it obvious where the expansion starts and ends, while single backticks make it more difficult to see where one's missing. And nesting...). Other possible reasons for a continuation prompt:

  • a missing done after while or for
  • a missing fi after an if
  • a missing esac after case
  • a missing closing parenthesis, e.g. in subshells (cd $HOME; cat .bashrc)
  • a missing command after piping | as well as conditional execution || and && (not & though, since that's just making the command running in background)
  • a missing closing quote (' or ")

Curiously enough, a missing brace } after a variable expansion ${ also causes a continuation prompt, but will fail due to the inserted space:

$ echo ${
> PS2}
bash: ${
PS2}: bad substitution

Tobias Kienzler

Posted 2017-04-25T22:03:08.497

Reputation: 3 262

2And the same thing happens when using quotes :) – Steen Schütt – 2017-04-26T09:20:40.537

1I already knew the answer to this question when I saw it in the sidebar but I wanted to give you an extra, albeit, imaginary +1 for a very nice in-depth answer. This is the sort of content that makes the StackExchange sites awesome. So... Thanks and keep being awesome. – Unkwntech – 2017-04-27T00:43:39.057

4

It means that your command is not complete yet. In fact, the character backtick, `, is used to delimit an inline command.

Example:

cd /tmp # Go to /tmp
pwd # Prints the current working  directory
ls `pwd` # Lists the content of the current working directory

Slyx

Posted 2017-04-25T22:03:08.497

Reputation: 213

0

The backtick starts/ends command substitution. The shell reads the command inside the backticks, interprets the results, and places those results into the command. E.g:. ls which cp results in the "which cp" being executed and the result of that command, i.e, /bin/cp will be used as the argument to the ls command. This behavior is, however, deprecated, in favor of the more explicit and readable $(cmd) command substitution.

When typing cd ` The shell is expecting a command to be executed to follow, and, to be close with a backtick.

In the case of the variable expansion using braces the shell presents the continuation prompt because those braces are lexically evaluated before the expansion is done.

Nicole Stevens

Posted 2017-04-25T22:03:08.497

Reputation: 1