This guide explains how to combine Apache Ant with Unix shell scripts for build tasks such as setting file permissions and processing files. It is aimed at engineers maintaining legacy Ant-based automation who need reliable, repeatable behavior in CI/CD environments.
Originally published on 2009-06-20; fully revised and updated.
Original post for reference: Ant Script with Shell script, How to run shell script from Ant Script?.

What this Ant + shell integration typically solves
- Set execute permissions on scripts before running them.
- Call shell utilities that are not available as native Ant tasks.
- Run file cleanup or text normalization during builds.
Choose the right approach: native Ant task vs shell execution
Prefer native Ant tasks when they exist, and use shell commands through exec when you need OS-specific tooling.
| Approach | Best for | Advantages | Caveats |
|---|---|---|---|
Ant chmod task | Setting Unix permissions on files/directories | Portable inside Ant builds; easy to read | Unix-like environments only |
Ant exec calling a script file | Reusable, multi-step shell logic | Keeps shell logic versioned and testable | Requires shell/runtime on build agent |
Ant exec with inline -c command | Small one-liners | Fast to add | Harder to maintain and quote safely |
Execution flow when Ant runs shell commands
flowchart TD
A[Ant target starts] --> B{Native Ant task available}
B -- Yes --> C[Run built in Ant task]
B -- No --> D[Use exec task]
D --> E[Call shell script with args]
E --> F[Script returns exit code]
F --> G{Exit code is zero}
G -- Yes --> H[Continue build]
G -- No --> I[Fail build when failonerror is true]
Set file permissions directly in Ant
Use Ant’s chmod task to ensure scripts are executable before calling them.
<project name="example" default="fix-perms" basedir=".">
<target name="fix-perms">
<chmod perm="755">
<fileset dir="${basedir}/scripts">
<include name="**/*.sh"/>
</fileset>
</chmod>
</target>
</project>Code language: HTML, XML (xml)
Run a shell script from Ant
Call a script file (recommended)
<target name="run-cleanup" depends="fix-perms">
<exec executable="/bin/sh" failonerror="true">
<arg value="${basedir}/scripts/cleanup-crlf.sh"/>
<arg value="${basedir}/input"/>
</exec>
</target>Code language: HTML, XML (xml)
Run a one-liner inline
<target name="list-text-files">
<exec executable="/bin/sh" failonerror="true">
<arg value="-c"/>
<arg value="find '${basedir}/input' -type f -name '*.txt' -print"/>
</exec>
</target>Code language: HTML, XML (xml)
Example cleanup script for carriage-return removal
The original post referenced removing special characters. The script below shows a safer pattern for removing carriage returns from text files and replacing files atomically.
#!/usr/bin/env bash
set -euo pipefail
root_dir="${1:-.}"
find "$root_dir" -type f -print0 | while IFS= read -r -d '' file; do
tmp="${file}.tmp"
tr -d '\r' < "$file" > "$tmp"
mv "$tmp" "$file"
echo "updated: $file"
doneCode language: PHP (php)
chmod +x scripts/cleanup-crlf.sh
ant run-cleanup
Best practices
- Use
failonerror="true"so shell failures stop the build immediately. - Keep complex shell logic in versioned
.shfiles, not inline command strings. - Apply cleanup only to known text files to avoid corrupting binaries.
- Use Ant properties for paths instead of hardcoding directories.
- Validate shell availability (
/bin/shorbash) on each CI runner image.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals
When Ant and shell scripts are used together, the real complexity comes from maintaining consistent behavior across different environments. A script that works correctly on one machine may fail on another due to differences in operating systems, permissions, installed utilities, or environment configurations. These dependencies can become difficult to troubleshoot when automation grows and more teams rely on the same build process.
For long-term stability, teams should focus on making these workflows easier to manage by adding clear error handling, meaningful logs, and proper documentation. As organizations modernize their delivery processes, reviewing older script-based automation and improving its structure can help reduce maintenance effort and avoid unexpected failures in CI/CD pipelines.