77

I am writing some documentation in markdown and I want to document how to create a text file using a bash HEREDOC. Here is the command I want to document:

# cat > /tmp/answers.txt <<EOT
> value1=blah
> value2=something else
> value3=`hostname`
> value4=onetwothree
EOT

In markdown one uses the ` to render the text as "code" I have tried doing this ...

`# cat > /tmp/answers.txt <<EOT`
`> value1=blah`
`> value2=something else`
`> value3=\`hostname\``
`> value4=onetwothree`
`EOT`

... but that results in something that looks like this ...

# cat > /tmp/answers.txt <<EOT
> value1=blah
> value2=something else
> value3=\

hostname
> value4=onetwothree
EOT

4 Answers 4

107

The original Markdown syntax documentation covers this; it says that you have to use multiple backticks to bracket the code expression, so like this:

``here you go - ` this was a backtick``

renders like this:

here you go - ` this was a backtick

If you want to include a backtick in normal text, not in a code block, a backslash escape does the trick; for example this:

Here's a backtick: \`; then, here's another one: \`

renders like this:

Here's a backtick: `; then, here's another one: `

As a commenter asked, “How can I have a code expression that contains only a backtick, or a sequence of multiple backticks?”

A single backtick can be written inline like this: `` ` ``

which, as you can see, renders like this: `

The rules is, if you want to write a sequence of backticks, the enclosing backticks (which must be written without whitespace between them) must be a different number of backticks from the quoted backticks. It does not have to be a greater number, only a different number. For example, seven backticks can be written like this: ` ``````` `

which then renders like so: ```````

Any number of consecutive backticks may be used as a code block delimiter. For example, an alternate form of the single backtick would be this: `````````` ` ``````````

which renders like so: `

(I tested this on commonmark and github and it behaves the same so it's not just a Stack Overflow oddity)

7
  • This was helpful to me, but I'd also add to your instructions that your line can't end with three backticks. E.g. I was trying to surround eval `ssh-agent -s` with pairs of double backticks and eventually realized that I needed to add a space before the closing pair of backticks.
    – Ryan
    Jul 1, 2018 at 14:58
  • 2
    I think you should also add that you can have a single space after the first `` and before the final one too, to allow escaping multiple backticks.
    – geekley
    Jul 10, 2018 at 15:46
  • How to render <pre>`(func)</pre> in a code block?
    – tejasvi88
    Mar 19, 2021 at 13:30
  • I can't show this in a comment, because in comments you can't use triple-backticks. But the answer above, "use double backticks around the <pre>" will work fine.
    – Glyph
    Mar 21, 2021 at 1:21
  • Thanks @geekley. I was trying to style a code one-liner that included two backticks, with the second at the very end of the expression. The double-backtick trick didn't work on GitHub without spaces inside the double-backticks. As you say, the spaces need to be balanced so the space character doesn't get rendered.
    – jdunning
    Jan 22, 2022 at 20:58
17

This code block below does the trick.

```
# cat > /tmp/answers.txt <<EOT
> value1=blah
> value2=something else
> value3=`hostname`
> value4=onetwothree
EOT
```

The three Backtick means it's snippet of code and a snippet must end with three more Backtick.

For more help with Markdown refer this CheatSheet.

10

I think you need to change the "delimiter" from a single back tick to a double...

i.e.: ``value3=\`hostname\` ``

should render

> value3=\`hostname\`

3
  • 1
    Thanks for the reply but that didn't solve my problem. I think I'll go with > value3=$(hostname) and give up on using old school backticks in my scripts. Jun 19, 2014 at 18:03
  • @RedCricket: I updated my commented with better formatting. Not sure if that will help you but you can use as many back ticks as you need to start/end a line. If you use 3 (for example) then 1 or 2 back ticks are rendered correctly. I think your solution will work as well Jun 19, 2014 at 18:25
  • 1
    @RobertEkendahl this worked for me. I think the general solution to the problem is that if you want to use back-ticks inside the code block, you can't fence it with single back ticks. you need double or triple. Thanks!
    – user44484
    Apr 6, 2015 at 15:48
3

Addendum: At least on SO, when the code starts or ends with `, you need to add spaces inside of backticks on both sides.

That is, if you want foo`, you must spell it as `` foo` ``, not as ``foo` ``, because the latter incorrectly renders as foo` .

At most one whitespace is removed in this manner (from each side), so ` foo ` renders as foo .

1
  • 1
    Thank you. This answer helped me the most because my string ended with a backtick and I didn't want that extra space.
    – Mayron
    Mar 26 at 11:07

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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