Skip to content

Instantly share code, notes, and snippets.

@daturkel
Created April 6, 2026 16:06
Show Gist options
  • Select an option

  • Save daturkel/1fde99f92b0e32dc078083c2ae716372 to your computer and use it in GitHub Desktop.

Select an option

Save daturkel/1fde99f92b0e32dc078083c2ae716372 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# ///
"""Collapse a shell command with line-continuation backslashes into a single line."""
import re
import sys
def fix_command(raw: str) -> str:
"""Join shell line continuations into a single executable command.
Args:
raw: Multi-line shell command with trailing backslash continuations.
Returns:
Single-line command ready to paste into a terminal.
"""
# Strip trailing whitespace (including invisible chars) from each line,
# then join continuation lines (ending in backslash) with a space.
lines = raw.splitlines()
joined: list[str] = []
for line in lines:
line = line.rstrip()
if line.endswith("\\"):
joined.append(line[:-1].rstrip())
else:
joined.append(line)
# Re-join: each stripped continuation becomes a space-separated token
result = " ".join(part.strip() for part in joined if part.strip())
return result
def main() -> None:
if not sys.stdin.isatty():
raw = sys.stdin.read()
elif len(sys.argv) > 1:
raw = " ".join(sys.argv[1:])
else:
print("Usage: fixcmd < command.txt OR pbpaste | fixcmd", file=sys.stderr)
sys.exit(1)
print(fix_command(raw))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment