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:

#!/bin/bash 
#Filename: cecho.sh 

echo $*'#'  

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

    $ ./cecho.sh 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, cecho.sh). I need to apply the arguments in several styles. In the first method, I need one argument for each invocation, like this:
        ./cecho.sh arg1 
        ./cecho.sh arg2 
        ./cecho.sh 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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.