12

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.

2 Answers 2

8

The following works in Ubuntu:

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.

Alternatively:

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

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

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

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

How about

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .