When I type
cd `
into a terminal, then I get this
>
Even ls
or cd
is not working. Attempting to run such a command on the >
prompt just gives another >
prompt.
Please explain the meaning of the above command. And how do I exit it?
When I type
cd `
into a terminal, then I get this
>
Even ls
or cd
is not working. Attempting to run such a command on the >
prompt just gives another >
prompt.
Please explain the meaning of the above command. And how do I exit it?
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?
a=`echo $b`
is in most cases a bad example. You will get the value of $b
, with all sequences of whitespace replaced with single spaces, then each word will be expanded as a glob based on the files of the current directory, and then all trailing newlines will be trimmed, and a single newline will be added in the end. I'd say that 99.9% of the time you'd want a=$b
, and the rest a=$(echo "$b")
if you want a single trailing newline.
– user000001
May 06 '19 at 20:59
a=`expr 1 + 3`
example? (Formatting in comments sucks!!!)
– Kulfy
May 06 '19 at 21:04
a=$((b+3))
in real code instead of expr
;)
– user000001
May 06 '19 at 21:09
`
and >
, I don't think such complain would occur ;-).
– Kulfy
May 06 '19 at 21:20
expr
syntax because they're used to seeing it around so they think it's normal/expected/correct, vs standardized-since-the-1990s (and thus not at all a bashism) $(( ... ))
POSIX math syntax. date
is a lot more defensible as an example, since it's legitimately needed when writing code for POSIX shells (or versions of bash that aren't recent 4.x adding printf '%(...)T'
) today.
– Charles Duffy
May 06 '19 at 21:57
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)"
>
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.
, tells the shell that you are starting a _command substitution_ and
>` means that the shell wants you to complete it. – John1024 May 06 '19 at 20:28