Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments with spaces between (bash) script

Tags:

bash

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
like image 568
John Fear Avatar asked Jun 13 '13 18:06

John Fear


People also ask

How do you pass space separated arguments in shell script?

You should just quote the second argument. Show activity on this post. If calling from any Unix shell, and the parameter has spaces, then you need to quote it. You should also quote every variable used within the function/script.

How do you pass a space argument?

You can put double-quotes around an argument in a java command, and that will allow you to pass arguments with spaces in them.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.


1 Answers

$*, 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.

like image 182
chepner Avatar answered Sep 21 '22 19:09

chepner