Skip to content

Instantly share code, notes, and snippets.

@balanza
Created October 29, 2025 20:38
Show Gist options
  • Select an option

  • Save balanza/3729dbc50bb989f4b8ab0e82bbcce78b to your computer and use it in GitHub Desktop.

Select an option

Save balanza/3729dbc50bb989f4b8ab0e82bbcce78b to your computer and use it in GitHub Desktop.
shell: replay
#!/bin/sh
# Replay N commands from history, in sequence
# Usage:
# replay <start command index> <number of commands to replay>
replay() {
local A="$1"
local N="$2"
local end=$((A + N))
# get current shell
local shell="${SHELL:-/bin/sh}"
# history output: skip first field (the number), preserve spaces
history | awk -v start="$A" -v end="$end" '$1 >= start && $1 <= end { $1=""; print substr($0,2) }' | while read -r cmd; do
echo "Running: $cmd"
"$shell" -i -c "$cmd"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment