Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

sort --compress-program=/bin/gzip works fine, but how can I pass options down to the compression programm (e.g. --best or --fast)?

sort --compress-program=/bin/gzip\ --best failes with the error message

couldn't execute compress program

as do sort --compress-program="/bin/gzip --best" and sort --compress-program=/bin/gzip\\\ --best (as a desperate guess).

Google-ing the error returned nothing but the source code generating the message.

Is there any way to do this without writing a wrapper or defining an alias?

share|improve this question
Pretty sure a wrapper script is the only way to do this - I imagine sort just treats whatever it gets as the binary name and doesn't try to split arguments. – Graeme Apr 15 '14 at 11:42
@Graeme: It just feels totally over-complicated to write a script consisting of 2 words + a shebang, make it executable and use this to raplace such a common tool. – mschilli Apr 15 '14 at 11:45

Many of the compressors take an an environment variable to accept options that cannot be passed on the command line. In your case

GZIP_OPT=-9 sort --compress-program=/bin/gzip 

The same is true for xz with XZ_OPT and bzip2 with BZIP2

share|improve this answer
Nice suggestion, this seems to work, too. However, as pointed out by @devnull, this option is not named consistantly and to me it feels as hacky and over-complicated as writing a wrapper or setting up an alias. – mschilli Apr 15 '14 at 11:49
@devnull: the export shouldn't be necessary if you define the environment variable on the same line as in @bdowning's answer. – mschilli Apr 15 '14 at 11:52
Another downside of this solution is that if you pass the option from a calling script/Makefile, you need to a) split it and b) figure out the name of the environment variable. I would like to be able to have a parameter like gzip --best or xz -0 that I just pass down to sort. But anyhow, thx for your fast reply and a nice idea. – mschilli Apr 15 '14 at 11:54
The compressors added this specifically to solve this class of problem for arg passing. It's not a hack, that's how they solved a problem not of their making. Only the {t}csh variants require an export. – bsd Apr 15 '14 at 12:14
But still, it seems impossible to write a shell script that can take gzip --best or xz -o as a parameter and use this as compression tool for sort. If I was to do this using your solution, I would have to know all possible compression tools and the corresponding environment variables. If somebody writes a my_compress that works STDIN to STOUT and supports -d for de-compression it would be impossible for this user to run my script passing my_compress --method=rle as the compression method to be used by sort. So they didn't solve the problem, they just hacked around it. ;) – mschilli Apr 15 '14 at 13:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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