Turning a Common Terminal Mistake into a Feature

Hey everyone,

Today's post is about a simple little shell script that solves a problem I'm sure many of us have run into. We've all been there: you're following a tutorial online and you blindly copy a command to paste into your terminal. The example looks something like this:

$ curl https://example.com

If you're not paying attention, you accidentally copy the $ prompt character along with the command. When you paste it and hit enter, your shell throws an error like $: command not found. It's a minor but frequent annoyance.


A Simple, Foolproof Solution

To solve this for myself, I wrote a simple script that I named $ and placed it in my path (~/.local/bin). Now, instead of an error, my shell gives me a helpful confirmation prompt. I call it the "Hey, you might have made a mistake" prompt.

Here's the script:

#!/usr/bin/env bash

# Simple command wrapper that confirms before running pasted commands

set -euo pipefail

main() {
  local status=0

  # Reconstruct the command as a safely quoted string for display
  local cmd_str=""
  for arg in "$@"; do
    if [ -z "$cmd_str" ]; then
      printf -v cmd_str "%q" "$arg"
    else
      printf -v cmd_str "%s %q" "$cmd_str" "$arg"
    fi
  done

  # Standard prompt and confirmation
  echo "It looks like you pasted a command prefixed with '$'."
  echo "Command:"
  echo "  $cmd_str"
  echo
  read -r -p "Do you want to run this command? [y/N] " reply
  case "$reply" in
  [yY] | [yY][eE][sS]) ;;
  *)
    echo "Canceled."
    return 0
    ;;
  esac

  # Execute the actual command
  "$@" || status=$?

  if [ $status -eq 0 ]; then
    echo "Command completed successfully."
  else
    echo "Command failed with exit code $status."
  fi
  return $status
}

main "$@"

Now, when I accidentally paste a command with a leading dollar sign, I get this instead of an error:

Hey dumbass, don't blindly paste things you see on the internet!

The script simply takes all the arguments passed to it (which is the command you intended to run), asks if you want to execute it, and then runs it if you confirm. It's a tiny quality-of-life improvement that turns a common mistake into a helpful interaction.

As always,
Michael Garcia a.k.a. TheCrazyGM

0.08020830 BEE
4 comments

This copy paste mistake I have actually made more than I care to admit! I even remember figuring out what that dollar sign was all about 😅

!PAKX
!PIMP
!PIZZA

0.00017788 BEE

View or trade PAKX tokens.

@ecoinstant, PAKX has voted the post by @thecrazygm. (1/2 calls)



Use !PAKX command if you hold enough balance to call for a @pakx vote on worthy posts! More details available on PAKX Blog.

0.00017439 BEE
(edited)

PIZZA!

$PIZZA slices delivered:
steemmillionaire tipped thecrazygm
@ecoinstant(1/20) tipped @thecrazygm

Come get MOONed!

0.00017083 BEE

Elegant solution! Love that you share these little puzzles!

!PIMP & !PIZZA

0.00016740 BEE

That's a great simple and useful tool. Taking into account possible mistakes, and automating error checking and correcting, is brilliant, my friend. 😁 🙏 💚 ✨ 🤙

0.00016404 BEE