$ considered harmful
Tags: productivity, shell
Published: 2020-07-04 05:23:38

---
title: $ considered harmful
published: true
description: Should you be using $ or not when writing shell documentation? A twitter thread and a mild early morning rant.
tags: productivity, shell, 
cover_image: https://images.unsplash.com/photo-1563296299-cc5a1e8bd708?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9
canonical_url: "https://nobitagit.github.io/blog/569JNRXZghiklmsconsidered-harmful/"
---
One of the most satisfying feelings in life is waking up at 5am, going to the kitchen to drink a glass of water while casually scrolling through Twitter on your phone and finding someone (on the Twitter feed, not in the kitchen) that is right on the internet*:

{% twitter 1277199020268703744 %}

_* Where right means: someone else is saying something you always thought of, but never quite found time/space/willpower/occasion to express to a wider audience or even to yourself._

![Morning duties](https://dev-to-uploads.s3.amazonaws.com/i/9hbwbe4ej5shyzw7nand.jpg)

## Disclaimer

Let's begin with a disclaimer: this is a clear case of first world problem that really has no great meaning in the grand scheme of things, and the arguments against it are still valid. I just found it amusing to see I wasn't really the only one being mildly frustrated by it.

I also think that the discussion that ensued is interesting and worth reading.

## Problem

When setting up a project you might need some setup and most of the times this involves running a handful of shell command.  An example:

```
$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
$ pyenv install 3.7.6
$ pyenv local 3.7.6
```

Ok, now try copy pasting those 3 lines in one go... Chances are you will see as output something like this:

```
zsh: command not found: $
```

Isn't this irritating?

## What does `$` even mean

There are a few reasons why `$` is prepended to every line of a shell command example:

- it indicates that indeed, what you are looking at is a command that is meant to be run in a shell
- It indicates that you need to run this command as a user, and not as root

## Why you should **not** remove `$`

Some valid arguments in favour of keeping the $:

- You should never trust commands on the internet and blindly copy paste them

{% twitter 1277464720728035331 %}

- You should communicate when to run a command a root (using `#`) and when not (`$`)

{% twitter 1277201037321998338 %}

## The case for removing `$`

- makes it harder to copy, especially multiline commands
- not everyone might be aware of # meaning "run as root"

In the  thread another user makes an great point

{% twitter 1277990481884901376 %}

## How to remove `$`

If you have read so far and sort of agree that we might be better off not including the `$` in shell samples, here's a few ways to make it happen.

### The easy way

Just don't type it! This applies to Github and pretty much all places where you have no control over how code samples are rendered. A post on Dev.to is one example.

So this
```sh
$ pyenv install 3.7.6
```
becomes
```sh
pyenv install 3.7.6
```
if a command is meant to be run as root prepend it with sudo

```sh
sudo apt-get install ubuntu-desktop  
```

### The stylish way

If you have control over the rendering and you still like you readers to see the `$`, consider using CSS to remove it from the text that ends up in the clipboard when copying. In the thread, Twitter user [@beccadottex](  https://twitter.com/beccadottex) shares a [Codepen](https://codepen.io/rebeccajae/pen/PoZJwav) of how this can be done. 

[@theconfigurator](https://twitter.com/theconfigurator) shares another one here 

{% twitter 1277279975654281216 %}

## Should you really stop using $?
As any other opinion based topic, there seems to be far from an agreement on the best practice around this, as the long thread on Twitter shows. 

As for myself, I always found it redundant and pointless but funnily enough [I found myself using it out of habit](https://dev.to/aurelio/up-your-lodash-game-by-going-functional-from-chaining-to-piping-3li8). From now on, I will purposefully keep the `$` out of my docs.

/rant

---

- _comic at the beginning is a ripoff of Xkcd's [Duty Calls](https://xkcd.com/386/)_
- _cover image is by [Ryan Quintal](https://unsplash.com/@ryanquintal)_.