1

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"

1
  • It's the same as the difference between echo hello and "echo hello" (try it). Commented Jun 21, 2016 at 8:50

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Your example cleared things. So basically, quotes will be treated as one single component.
1

"$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 :-).

2 Comments

Thank you for your explaination. Doesn't "$ANT_BUILD_PATH $1" mean "/path/to/ant release". What do you mean by concatenation of ant build path and parameter? Isn't final meaning of both is /path/to/ant release for shell?
Ye. But the shell has the idea of "strings", or "words" passed to, and it treats them separately. If you don't use the quotes, the default words separator is the space. If you use the quotes, they prevail over any space, so the shell gets a single word with spaces, which is not a vaild command...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.