Ubuntu – Print numbered arguments in Command Line Tool

command linedebuggingscripts

Is there a packaged program that shows command line arguments? I often find it helpful to have users run this type of program to either help them understand what their shell commands do, or to get clearer, less ambiguous feedback (e.g. when I can't see their screen and we're communicating online with paste sites).

I've written the below script in Python, which works, but I'd find it easier to tell people a Ubuntu package they can grab rather than explain how write to a file, chmod +x, and change their system (e.g. maybe they don't have ~/bin setup in PATH, …).

I'd be happy to see a command-line sed/awk/perl/etc. script to do this functionality as well, even if it's ugly. ("If"… doing this in one-line will be ugly.)

Any solution needs to have somewhat similar results to this script; which are, in English: print each argument with a number (escaping problematic characters is just a bonus).

#!/usr/bin/env python2.6
import sys

if len(sys.argv) == 1:
  print "No args!"
else:
  args = enumerate(sys.argv)
  args.next()  # skip program name
  for i, v in args:
    r = repr(v)
    if v.strip() != v or v != r[1:-1]:
      v = r + "\t(repr)"
    print "%3d: %s" % (i, v)

Best Answer

My solution (you already know ;) as a function (no need to deal with PATH) to become one-liner:

showargs() { i=1; for f; do printf "arg%d: <%s>\n" $i "$f"; ((i++)); done; }