Ubuntu – Add flag to existing terminal command

command line

From this question: Will ls always list the files that rm will remove? @fkraiem commented and said:

I am somewhat surprised that rm does not have a –dry-run flag…

This reminded me of a scenario I had sometime back using the seq command to generate a sequence of numbers from 1 – 100. The problem was that the list had a prefix Item

seq 100

Generated:

1
2
3
...
100

Whereas I wanted:

Item 1
Item 2
Item 3
...
Item 100

Using the seq command as an example, is there anyway I can add a --prefix=[PREFIX] flag that automatically adds a prefix to the generated sequence list?

I am not talking about bash aliases

Best Answer

In general, short of modifying the command's code (and recompiling, in the case of a compiled executable) you would need to write a wrapper function - either as a shell function or an executable script located ahead of the original executable in your PATH. It would need to:

  • parse the command line arguments
  • intercept your additional option (and any option-specific argument)
  • perform the new action
  • otherwise pass the remaining arguments to the original command

However your seq should already have an option to specify the number format, which may be (ab)used to get the kind of list you want

   -f, --format=FORMAT
          use printf style floating-point FORMAT

Ex.

$ seq -f 'Item %.0f' 1 10
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10

More generally, you could use printf e.g. printf 'Item %d\n' $(seq 1 100) or (using bash's built-in brace expansion) printf 'Item %d\n' {1..100}


BTW rm does have a sort of dry-run flag, -i

-i     prompt before every removal

or (less portably)

-I     prompt once before removing  more  than  three  files,  or  when
      removing recursively; less intrusive than -i, while still giving
      protection against most mistakes