46

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

Red Cricket
  • 8,734
  • 16
  • 67
  • 148

3 Answers3

63

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: `

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

Glyph
  • 30,222
  • 9
  • 82
  • 119
  • 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 01 '18 at 14:58
  • 1
    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 '18 at 15:46
  • How to render
    `(func)
    in a code block?
    – tejasvi88 Mar 19 '21 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 `
    `" will work fine.
    – Glyph Mar 21 '21 at 01: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 '22 at 20:58
13

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.

Adi
  • 4,635
  • 1
  • 23
  • 37
9

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

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

should render

> value3=\`hostname\`

Robert Ekendahl
  • 267
  • 2
  • 10
  • 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. – Red Cricket Jun 19 '14 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 – Robert Ekendahl Jun 19 '14 at 18:25
  • @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! –  Apr 06 '15 at 15:48