8

How to execute bash script with parameters:

./foo.sh a b c

When it's compressed (e.g. using xz).

 xzcat foo.sh | bash <<how_to_supply_here_parameters?>>

Specific usecase:

I produced very big rmlint.sh file and store it compressed:

time rmlint -o sh:stdout -c sh:hardlink|tee >( xz > rmlint.sh.xz )

Therefore I would normally execute

./rmlint.sh -d -x -p

However, file is too big to be uncompressed. Therefore I would love to do same by pipe-ing it to bash:

xzcat rmlint.sh.xz | bash ...
1
  • 3
    How big is this script, is it a world record? Did you use functions, or paste/copy code all over the place? Dec 27, 2018 at 10:08

1 Answer 1

16

You should use the -s option and -- to separate arguments you want to pass:

echo 'echo "$@"' | sh -s 3 4 5

echo 'printf "{%s}" "$0"; printf " {%s}" "$@"; echo' |
    sh -s -- -d -x -p --foo=bar
{sh} {-d} {-x} {-p} {--foo=bar}

This should work with any POSIX shell, not just bash. From susv4:

-s Read commands from the standard input.

If there are no operands and the -c option is not specified, the -s option shall be assumed.

4
  • There is a problem with this, as my parameters have "-" prefix, therefore I call bash -s -d -x -p and get in return : bash: -d: invalid option ( pastebin.com/JXRiUuLR ) Dec 27, 2018 at 10:47
  • 2
    You use the -- end of options marker, as usual. See the 2nd example.
    – mosvy
    Dec 27, 2018 at 10:57
  • Thank you a lot for help and apologise for stupid overlook! You helped me to find what I was overlooking (to short sleep Today), which was -- - Thank you! Now it works perfectly! Dec 27, 2018 at 11:01
  • Added -- to first line of your solution as it was something I overlooked, so I hope it will help some others, who may overlook it as well Dec 28, 2018 at 10:42

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