DEV Community

Cover image for $ considered harmful
Aurelio
Aurelio

Posted on • Originally published at nobitagit.github.io

$ considered 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*:

* 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

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
  • You should communicate when to run a command a root (using #) and when not ($)

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

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

$ pyenv install 3.7.6

becomes

pyenv install 3.7.6

if a command is meant to be run as root prepend it with sudo

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 shares a Codepen of how this can be done.

@theconfigurator shares another one here

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. From now on, I will purposefully keep the $ out of my docs.

/rant


Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair

I've pondered this myself. I use it in embedded scripts-that-aren't exactly scripts. Stuff where I'm giving an example of what something will do when run.

For example, in a recent post I had something like this:

$ pwd
/home/moopet

$ echo $$
8192

I don't want to have to explain what I'm doing every time, and $ is a kinda convention, but I'm also slightly concerned that it might confuse people who haven't seen it before.

As far as presenting literal lines as a series of instructions, I think we should leave it out. Not so that people can copy-paste the whole script, though! We should encourage people to try one line at a time, because these little formulae, even with the best intention, will explode your machine one day from a casual assumtpion:

cd /tmp/foo
rm -rf *

In the real world, we'd want to at least separate those commands with && or something, in case /tmp/foo doesn't exist.

So I think any argument for copying snippets small enough for tweets is probably invalid.

Collapse
 
kallmanation profile image
Nathan Kallman • Edited

Or just solve this for yourself so you can copy paste commands even if people do continue to use the $

Collapse
 
datamuc profile image
datamuc
printf '%s\n' '#!/bin/sh' 'exec "$@"' > ~/bin/$
chmod a+rx ~/bin/$