109

I've got the following bash two scripts

a.sh:

#!/bin/bash
./b.sh 'My Argument'

b.sh:

#!/bin/bash
someApp $*

The someApp binary receives $* as 2 arguments ('My' and 'Argument') instead of 1.

I've tested several things:

  • Running someApp only thru b.sh works as expected
  • Iterate+echo the arguments in b.sh works as expected
  • Using $@ instead of $* doesn't make a difference
John Fear
  • 1,265
  • 2
  • 8
  • 10

1 Answers1

167

$*, unquoted, expands to two words. You need to quote it so that someApp receives a single argument.

someApp "$*"

It's possible that you want to use $@ instead, so that someApp would receive two arguments if you were to call b.sh as

b.sh 'My first' 'My second'

With someApp "$*", someApp would receive a single argument My first My second. With someApp "$@", someApp would receive two arguments, My first and My second.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 31
    The key that is easy to miss is that "$@" needs to be quoted, it seems. $@ is not enough. – miracle2k Jun 21 '14 at 11:48
  • 2
    @miracle2k Correct. Unquoted, `$@` and `$*` work identically. – chepner Jun 21 '14 at 14:19
  • 3
    @Matt That is something entirely different. "$@" is special in that it quotes each argument separately. It can result in more than one argument thus. "$something" is just quotes around whatever $something expands to and will always be a single argument. – Carlo Wood Jan 26 '17 at 12:59
  • 2
    The answer only works for me if I set `IFS=$'\n'`. No idea why. – Jessie Koffi May 20 '19 at 16:02