Dhruv Jagetiya Dhruv Jagetiya - 5 months ago 21
Bash Question

Passing argument to command in bash shell script

I was trying to automate a small build process using shell scripting. Build process is basically:


  1. cd /location/to/build.xml

  2. /Path/To/ant release. Run ant with release as argument.



I created a build.sh file which I intend to use as
./build.sh release
to build release version, and my shell script file is:

ANT_BUILD_PATH="/Path/To/ant"
cd "/Location/To/Build.xml"
"$ANT_BUILD_PATH $1"


I get
ant not a file or command
when I execute this shell file as
./build.sh release
even though the ant file is there.

Also, when I use
"$ANT_BUILD_PATH" "$1"
. Script runs just fine.

What is the difference between
"$ANT_BUILD_PATH $1"
and
"$ANT_BUILD_PATH" "$1"

Answer

The different is, when you do "$ANT... $1", shell execute a command called /path/to/ant release, note, it is one command with space as part of command. So command not found.

But if you "$ANT..." "$1" it will execute command /paht/to/ant, and take release as argument/option.

Test with this, you will see:

kent$  "ls -l"
zsh: command not found: ls -l #it thinks a command is "ls(space)-l"

kent$  "ls" "-l"
total xx
<file lists>