How to pass pipe output as arguement to a command

When  working with shell scripting task, one often encounters situation where one command’s  output need to be passed to another command and in such situation typically pipe feature is used.

ls -1|wc -l

The above example will list the number of files in the current directory.

if you want to pass the filename as command line arguement instead of passing it to stdin then the below method should be used.

ls -1|xargs -n 1 echo "The file name is" $1

Here the output of ls -1 will be passed as a command line arguement to the echo.
For more information refer to man page or wiki page of xargs.

Leave a Reply

Your email address will not be published. Required fields are marked *