Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
how do I get markdown to render # as a shell prompt and not a comment?
Note: This is a general question about Markdown formatting for any Markdown renderer (e.g., Gitlab, Github, Codidact). So this is not just a question about Codidact's renderer.
When displaying shell commands in Markdown, I often want to use the hash sign (#
) to indicate that I am running the command as root. However, Markdown renderers usually interpret the #
character as the beginning of a comment, which results in the entire line being rendered as a comment (usually in the color gray).
For example, using the shell code fence (```sh), the following command is rendered as intended with colors:
echo hi
However, if I precede that command with #
, the Markdown interpreter thinks the entire line is a comment:
# echo hi
Is there anyway to tell Markdown to ignore/exclude the #
character?
2 answers
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Trevor | (no comment) | Dec 6, 2024 at 19:32 |
Instead of sh
, use shell
or console
.
So this:
```shell
# echo hi
```
```console
# echo hi
```
Is rendered as:
# echo hi
# echo hi
While this:
```bash
# echo hi
```
```sh
# echo hi
```
Is rendered as:
# echo hi
# echo hi
Below are the same code blocks as images (just in case the highlight engine[1] changes in the future).
With shell
and console
:
With bash
and sh
:
That might be a little bit confusing, but bash
and sh
refers to "shell script" (which treats #
as a comment), while shell
and console
refers to a "shell session", hence they treat the initial #
as the prompt.
-
Currently, Codidact uses highlight.js ↩︎
0 comment threads
Note that, by declaring sh
as the language, you have implicitly declared "a #
introduces a comment", because that's what is implied by the sh
syntax.
But you say you don't want that interpretation. Therefore, the text you're formatting should not be interpreted as sh
language text.
So, to avoid sh
interpretation of your text, you need to avoid declaring sh
as the language of your text.
Rather, you are presenting a plain text example: text that the person might see in their terminal console, including the #
prompt before input.
That's done with the console
declaration (maybe others).
```console
# echo hi
```
# echo hi
1 comment thread