r/ClaudeCode • • 3d ago

Tips & Workflows Here is a helpful mac script, it sends the last Claude suggested command line to your clipboard for easy paste

Claude explains how to port to Linux and Windows.

⏺ copy-cmd.sh works on a Mac only, as written. The reason is lines 64-65, which call /usr/bin/pbcopy and /usr/bin/pbpaste. Those exist only on macOS. Everything else in it (od, grep -E, printf, wc, bash) is standard and runs the same on Linux, and on Windows under Git Bash or WSL.

Linux: swap in a clipboard tool. There are three common ones:

- wl-copy / wl-paste on Wayland

- xclip -selection clipboard (copy, and -o to read back) on X11

- xsel --clipboard --input / --output

Also, line 12 sets LC_ALL=en_US.UTF-8, which many Linux boxes don't have installed. It would only print a locale warning, but C.UTF-8 is the safer choice there.

Windows:

- WSL: copy with clip.exe, and read back with powershell.exe -NoProfile -Command Get-Clipboard. Watch for two things. clip.exe doesn't keep UTF-8 cleanly, so non-ASCII can come out garbled. And PowerShell adds a CRLF when it reads back, so the script's exact read-back check would fail unless you strip it.

- Git Bash: the same commands work, with the same caveats.

- Plain cmd or PowerShell: a bash script won't run there at all.

To make it portable: pick the copy and paste commands at the top based on what's installed (pbcopy, then wl-copy, xclip, xsel, clip.exe). Refuse with a clear message if none is found, and normalize line endings before the read-back check.

#!/bin/bash
# copy-cmd.sh: put one command on the macOS clipboard so the user can paste it.
#
# Reads the command from stdin only. Any argument is a usage error, so shell
# quoting can never mangle what gets copied. Trailing newlines are stripped so
# a paste never runs by itself; everything else is kept byte for byte.
#
# Bash variables cannot hold NUL, so stdin is read as hex (od) and rebuilt with
# printf escapes. That lets control bytes be refused before the clipboard is
# touched, and keeps quotes, backslashes, $ and UTF-8 exact.

export LC_ALL=en_US.UTF-8

refuse() {
  echo "copy-cmd: refused: $1" >&2
  exit 3
}

if [ "$#" -ne 0 ] || [ -t 0 ]; then
  echo "usage: copy-cmd.sh <<'CMD' ... CMD   (command on stdin, no arguments)" >&2
  exit 2
fi

hex=$(od -An -v -tx1)
esc=""     # printf-escaped bytes of the text kept so far
pending=""  # newlines not yet known to be internal (trailing ones are dropped)
multi=0
for b in $hex; do
  case "$b" in
    0a)
      pending="$pending\\x0a" ;;
    00|01|02|03|04|05|06|07|08|0b|0c|0d|0e|0f|1[0-9a-f]|7f)
      refuse "control character 0x$b (only tab and newline are allowed)" ;;
    *)
      if [ -n "$pending" ]; then
        esc="$esc$pending"
        pending=""
        multi=1
      fi
      esc="$esc\\x$b" ;;
  esac
done

[ -n "$esc" ] || refuse "empty input"
if [ "$multi" -eq 1 ] && [ "${COPY_CMD_MULTILINE:-}" != "1" ]; then
  refuse "more than one line (set COPY_CMD_MULTILINE=1 to allow)"
fi

# esc holds only \xHH escapes built above, so using it as the format is intended.
# shellcheck disable=SC2059
input=$(printf "$esc"; printf x)
input=${input%x}

# Patterns only; a public SSH key (ssh-ed25519 AAAA...) matches none of them.
printf '%s\n' "$input" | LC_ALL=C grep -q -e '-----BEGIN' \
  && refuse "looks like a private key (-----BEGIN)"
printf '%s\n' "$input" | LC_ALL=C grep -q -E -e '(^|[^A-Za-z0-9])(sk-|ghp_|gho_|github_pat_|xox[bp]-)[A-Za-z0-9_-]{10,}' \
  && refuse "looks like an API token"
printf '%s\n' "$input" | LC_ALL=C grep -q -E -e 'AKIA[0-9A-Z]{16}' \
  && refuse "looks like an AWS access key id"
printf '%s\n' "$input" | LC_ALL=C grep -q -i -E -e '(password|passwd|token|secret)=[^ ]{6,}' \
  && refuse "looks like a password or token assignment"

printf '%s' "$input" | /usr/bin/pbcopy || { echo "copy-cmd: pbcopy failed" >&2; exit 4; }
back=$(/usr/bin/pbpaste; printf x)
back=${back%x}
if [ "$back" != "$input" ]; then
  echo "copy-cmd: clipboard read-back does not match what was copied" >&2
  exit 4
fi

n=$(printf '%s' "$input" | wc -m | tr -d ' ')
echo "copied ($n chars)"
0 Upvotes

5 comments sorted by

•

u/AutoModerator 3d ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Mr_Maffin 3d ago

wth... why would I need a script to copy the command from the CLI..?

1

u/calunicornia 3d ago

so you dont have to copy it? is there a way to auto copy i am unaware of?

1

u/Mr_Maffin 3d ago

I've never had an issue copying commands from the internet in decades of use, so I genuinely don't understand what problem this script is supposed to solve

1

u/calunicornia 3d ago

First you need to highlight the text which might be moving since claude is spitting more out. Then sometimes claude likes to put the command in between other words so you cant just double click the line to copy.

Claude refuses to do all sorts of admin stuff on my machine and others, especially sudo, so I got tired of the copy step and made this script so I can just paste into the terminal when Claude gives me a command. It happens many times per day.