-1

Say I have a executable myProgram, who takes a filename as one of the arguments, and output a lot of stuff to stdout. Now I want to write a bash script to invoke myProgram and pipe the output to less. Normally I would create a bash script myCommand:

#!/bin/bash
myProgram $* | less

But somehow bash will split any file name with spaces into multiple arguments. For example, it converts

myCommand some\ file -m 5

into

myProgram some file -m 5 | less

which of course causes error. Is there a way to resolve this problem? I tried adding " around $* but then realized that it won't help because it will cause the entire argument list becoming a single argument, which is not what I want.

xzhu
  • 5,675
  • 4
  • 32
  • 52
  • 1
    Replace `$*` with `"$@"`. – alvits Apr 20 '14 at 04:42
  • possible duplicate of [Handle whitespaces in arguments to a bash script](http://stackoverflow.com/questions/5564450/handle-whitespaces-in-arguments-to-a-bash-script) – Joe Apr 20 '14 at 04:44

1 Answers1

4

Replace $* with "$@"

myProgram "$@" | less

alvits
  • 6,550
  • 1
  • 28
  • 28