user3829913
user3829913

Reputation: 11

reading command line arguments through pipe to sh

I am running a shell script by piping it to sh. For example:

curl commands.io/count-duplicate-lines-in-a-file | sh

The only way I could figure out how to pass in the filename was to use:

read file </dev/tty

You can check out the script here: Count duplicate lines in a file

Is there another way to pass in the filename as an argument to the script without first saving it to a file locally, setting permissions and running it?

The idea is you can use Monitor to capture terminal input/output and then re-run it from the command line using curl piped to sh.

Upvotes: 1

Views: 1624

Answers (1)

konsolebox
konsolebox

Reputation: 75488

Use -s option:

echo 'echo "$@"' | sh -s 1 2 3 4

Output:

1 2 3 4

Another way is to use process substitution if shell supports it:

bash <(echo 'echo "$@"') 1 2 3 4

Upvotes: 2

Related Questions