user3690202
user3690202

Reputation: 4075

How to call an application in Bash using an argument with spaces

I have a bash file which is passed arguments which contain spaces. The bash file looks like:

#!/bin/bash
another_app "$1"

However, instead of processing the argument as a single argument, as I believe it should, it processes it as a number of arguments depending on how many spaces. For example, if I call my bash file such:

my_app "A Number Of Words"

Then the "another_app" application gets passed 4 different arguments, instead of one. How can I just pass the single argument through to the second application?

Upvotes: 0

Views: 46

Answers (2)

Cody Stevens
Cody Stevens

Reputation: 414

The others are correct it will depend somewhat on how the 2nd app handles the args. You can also have a little control as to how the args are passed. You can do this with some quoting or using the "$@" var as mentioned by @steve

For example app1.sh

#!/bin/bash 

echo "Argument with no quotes"
./another_app.sh $1

echo "Argument with quotes"
./another_app.sh "$1" 

echo "Argument with \$@"
./another_app.sh "$@"

and another_app.sh #!/bin/bash

echo "Inside $0" 
echo "Number of args passed to me: $#"

for X in "${@}"
do 
  echo $X 
done
echo "Exiting $0" 

Upvotes: 1

Steve Vinoski
Steve Vinoski

Reputation: 20024

Call the second application using "$@":

#!/bin/bash
another_app "$@"

Upvotes: 1

Related Questions