3

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 ...
  • 3
    How big is this script, is it a world record? Did you use functions, or paste/copy code all over the place? – ctrl-alt-delor Dec 27 '18 at 10:08
7

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.

  • 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 ) – Grzegorz Wierzowiecki Dec 27 '18 at 10:47
  • 2
    You use the -- end of options marker, as usual. See the 2nd example. – mosvy Dec 27 '18 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! – Grzegorz Wierzowiecki Dec 27 '18 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 – Grzegorz Wierzowiecki Dec 28 '18 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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