Is there any way to pass arguments via pipe on bash?

something like:

echo value1 value2 | mkdir -p ./$1/$2

The line above would create the directories ./value1/value2

If this is not possible I would accept suggestions.

Any help is appreciated.

up vote 2 down vote accepted

The following works in Unbuntu:

echo -e value1\\nvalue2|echo mkdir -p ./`line`/`line`

I can't guarantee that all bash implementations will read the lines in left-to-right order.


echo value1 value2|( read p1 p2; echo mkdir -p ./$p1/$p2 )

This will work with all bash implementations, but will need elaboration if either value contains blanks.

(I have used echo mkdir to show the effect.)

  • The second alternative is perfect for what I'm doing, thanks! – guest9586 Dec 11 '14 at 18:51

How about

echo v1 v2 | awk '{print "./"$1"/"$2}' | xargs mkdir -p

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.