Dhruv Jagetiya
Dhruv Jagetiya

Reputation: 1043

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"

Upvotes: 1

Views: 160

Answers (2)

Kent
Kent

Reputation: 195229

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>

Upvotes: 3

MarcoS
MarcoS

Reputation: 17721

"$ANT_BUILD_PATH $1" passes one string to the shell, composed by the concatenation of the ant build path and the parameter. This is not what you want.

"$ANT_BUILD_PATH" "$1" passes two strings to the shell: the ant build path as the first string, and the parameter as the second string. This is what you want :-).

Upvotes: 1

Related Questions