awslabs / awslabs/agentcore-samples

01-tutorials - [Bug] avoid unbound variable error for AWS_REGION in prereq.sh

Open
#553 0 comments 0 reactions 1 assignee Claimed by @akshseh View on GitHub
01-tutorials 07-AgentCore-E2E bug
Dominant language
Python
Stars
3.4k
Forks
1.3k
Avg merge
1d 22h
Merged PRs (30d)
30

Description

**In which component is this bug present?**

- [ ] 01-AgentCore-runtime
- [ ] 02-AgentCore-gateway
- [ ] 03-AgentCore-identity
- [ ] 04-AgentCore-memory
- [ ] 05-AgentCore-tools
- [ ] 06-AgentCore-observability
- [x] 07-AgentCore-E2E

If you are reporting multiple bugs, please create a separate issue for each. For documentation improvements, use the documentation improvement issue type.

**Bug Description**

When executing the shell script
/01-tutorials/07-AgentCore-E2E/scripts/`prereq.sh`
with set -euo pipefail, the script fails immediately if AWS_REGION is not defined:
```
./prereq.sh: line 14: AWS_REGION: unbound variable
```
This happens because the variable is referenced before it is defined, which violates the set -u rule.

---

Root Cause
The script uses `${AWS_REGION}` directly in this line:
```
if [ -z "${AWS_REGION}" ]; then
```

Under set -u, referencing an undefined variable throws an “unbound variable” error even inside test brackets.

---

Suggested Fix
Use parameter expansion `${AWS_REGION-}` to handle the case when the variable is unset:

```
if [ -z "${AWS_REGION-}" ]; then
REGION=$(aws configure get region 2>/dev/null || echo "us-west-2")
export AWS_REGION="${REGION}"
fi
```

This safely allows the region detection logic to run even when AWS_REGION is not pre-set.

**Screenshots**
Not applicable — this issue can be reproduced via command-line execution.
Execution trace for reference:
```
$ unset AWS_REGION
$ ./prereq.sh
./prereq.sh: line 20: AWS_REGION: unbound variable
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.