Passing formatted arguments to a command by reading stdin

Here is a small echo script to make it obvious as to how xargs provides command arguments:


echo $*'#'  

When arguments are passed to the shell, it will print the arguments terminated by the # character. Consider this example:

    $ ./ arg1 arg2
    arg1 arg2 #

Here's a common problem:

  • I have a list of elements in a file (one per line) to be provided to a command (say, I need to apply the arguments in several styles. In the first method, I need one argument for each invocation, like this:
        ./ arg1 
        ./ arg2 
        ./ arg3 
  • Next, I need to provide one or two arguments each for each execution of the command, ...

Get Linux Shell Scripting Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.