Skip to main content

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - to its own stdout. The output: nothing at all.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But what about the general case. What you really want is to convert stdout of one command to command line args of another. As others have said, xargs is the canonical helper tool in this case, reading itsthe command line args for a command from its stdin, and constructing commands to run.

ls | xargs echo

You could also convert this some, usinguse the substitution command $() to do what you want.

echo $(ls)

Would also do what you want.

Both of these tools are pretty core to shell scripting, you should learn both.

For completeness, as you indicate in the question, the other base way to convert stdin to command line args is the shell's builtin read command. It converts "words" (words as defined by the IFS variable) to a temp variable, which you can use in any command runs.

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - its own stdout. The output: nothing at all.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But what about the general case. What you really want is to convert stdout of one command to command line args of another. As others have said, xargs is the canonical helper tool in this case, reading its command line args for a command from its stdin, and constructing commands to run.

ls | xargs echo

You could also convert this some, using the substitution command $()

echo $(ls)

Would also do what you want.

Both of these tools are pretty core to shell scripting, you should learn both.

For completeness, as you indicate in the question, the other base way to convert stdin to command line args is the shell's builtin read command. It converts "words" (words as defined by the IFS variable) to a temp variable, which you can use in any command runs.

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - to its own stdout. The output: nothing at all.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But what about the general case. What you really want is to convert stdout of one command to command line args of another. As others have said, xargs is the canonical helper tool in this case, reading the command line args for a command from its stdin and constructing commands to run.

ls | xargs echo

You could also use the substitution command $() to do what you want.

echo $(ls)

Both of these tools are pretty core to shell scripting, you should learn both.

For completeness, as you indicate in the question, the other base way to convert stdin to command line args is the shell's builtin read command. It converts "words" (words as defined by the IFS variable) to a temp variable, which you can use in any command runs.

added 357 characters in body
Source Link
Rich Homolka
  • 31.7k
  • 7
  • 55
  • 80

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - its own stdout. The output: nothing at all.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But closer to what about the general case. What you really want is to convertingconvert stdout of one command to command line args of another. As others have said, xargs is the canonical helper tool in this case, reading it'sits command line args for a command, then from its stdin, and constructing commands to run.

ls | xargs echo

You could also convert this some, using the substitution command $()

echo $(ls)

Would also do what you want.

Both of these tools are pretty core to shell scripting, you should learn both.

For completeness, as you indicate in the question, the other base way to convert stdin to command line args is the shell's builtin read command. It converts "words" (words as defined by the IFS variable) to a temp variable, which you can use in any command runs.

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - its own stdout.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But closer to what you want is to converting stdout to command line args. As others have said, xargs is the canonical tool in this case, reading it's command line args for a command, then stdin and constructing commands to run.

ls | xargs echo

You could also convert this some, using the substitution command $()

echo $(ls)

Would also do what you want.

Both of these tools are pretty core to shell scripting, you should learn both.

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - its own stdout. The output: nothing at all.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But what about the general case. What you really want is to convert stdout of one command to command line args of another. As others have said, xargs is the canonical helper tool in this case, reading its command line args for a command from its stdin, and constructing commands to run.

ls | xargs echo

You could also convert this some, using the substitution command $()

echo $(ls)

Would also do what you want.

Both of these tools are pretty core to shell scripting, you should learn both.

For completeness, as you indicate in the question, the other base way to convert stdin to command line args is the shell's builtin read command. It converts "words" (words as defined by the IFS variable) to a temp variable, which you can use in any command runs.

Source Link
Rich Homolka
  • 31.7k
  • 7
  • 55
  • 80

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So

ls | echo

Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - its own stdout.

There are a few solutions in this case. One is to use a command that reads stdin and dumps to stdout, such as cat.

ls | cat

Will 'work', depending on what your definition of work is.

But closer to what you want is to converting stdout to command line args. As others have said, xargs is the canonical tool in this case, reading it's command line args for a command, then stdin and constructing commands to run.

ls | xargs echo

You could also convert this some, using the substitution command $()

echo $(ls)

Would also do what you want.

Both of these tools are pretty core to shell scripting, you should learn both.