Root CauseWhat broke, why, and the fix.

bash variable empty caused published Amazon links to lose their tracking ID

· Bash, debugging, WordPress
ⓘ Operated by TechAthletes. Every post here is a bug we hit in our own work — symptom, root cause, fix. Nothing is sponsored and we are not paid to mention any tool.

Key points

  • A publishing script expanded ${TRACKING_ID} before the assignment ran, leaving Amazon links with an empty tag= parameter. (Source: our own measurement, 2026-09-12)
  • The script reported successful publication without exposing the missing tracking value inside the articles. (Source: our own measurement, 2026-09-12)
  • The repair moved the assignment, added generated-link correction, and stopped publication when the tracking ID was empty. (Source: our own measurement, 2026-09-12)
  • Updating stored articles did not immediately update cached pages; ordinary requests still returned empty tags until the page cache was removed. (Source: our own measurement, 2026-09-12)

The articles published, but the tracking value was missing

An automated publishing script was posting 33 articles a day across eight sites. The visible publishing operation worked: articles appeared, and the script reported success. Inside those articles, however, Amazon affiliate links were being published with an empty tracking parameter.

The recorded destination included this URL:

https://www.amazon.co.jp/s?k=monitor+arm+desk+mount&tag=

The search terms were present. The tag parameter was present. The tracking ID after the equals sign was missing. The incident record identifies that missing ID as the reason clicks on the affected links generated no affiliate revenue.

An empty affiliate tag is a tag= URL parameter with no tracking ID after the equals sign.

The inventory recorded 1,040 published articles, including 152 classified as having empty tags and 98 classified as already tagged. Those categories did not account for the entire published inventory. The useful repair target was therefore the measured set of articles with empty tags; the publication total alone did not establish the condition of every article.

That distinction matters when describing the failure. A successful post and a correctly tagged affiliate link were separate outcomes. The script provided evidence of the former while the actual destination URL exposed the failure of the latter.

The link also made the defect concrete. This was not merely a concern about whether a configuration value looked correct in a script. The missing value had reached the published output.

(Source: our own measurement, 2026-09-12)

The assignment existed, which made the script look correct

The script contained code that assigned TRACKING_ID. Reading that assignment in isolation could suggest that the tracking configuration was already handled.

The problem was its position.

The article-generation prompt referenced ${TRACKING_ID} around line 43. The case statement that assigned the variable appeared around line 88, after the prompt had already used it. At the point of expansion, the value had not been assigned.

The shell evaluated the script in order. In this script, the unset variable expanded to an empty string inside the prompt. The later assignment did not supply a value to the earlier expansion.

Another detail made the assignment block misleading: CATEGORY, configured alongside the tracking ID, was used later. TRACKING_ID was not used anywhere after its assignment. The neighboring category configuration had an effect, while the tracking assignment had no later consumer.

The presence of working configuration beside ineffective configuration helped conceal the ordering error. Finding the variable name and its assignment was insufficient. The necessary check was whether the assignment happened before the particular use that needed its value.

The script also lacked set -u, so the unset reference did not stop execution with an error. The Bash Reference Manual documents nounset (-u) as exactly the setting that makes the shell treat expansion of an unset variable as an error instead of substituting an empty string. Instead, the empty value continued into the prompt.

Finally, the publication log contained only a success message in this form:

✅ Success: <URL>

That message accurately reflected the successful posting operation. It did not inspect or report the affiliate tracking value inside the posted article. Nothing in that success line distinguished an article with the intended tag from an article with an empty one.

(Source: our own measurement, 2026-09-12)

The repair addressed assignment order and generated output

The direct code repair was to move the case statement before the generation prompt. That placed the tracking assignment ahead of the expansion that needed it.

This addressed the recorded root cause: the prompt had been constructed before the tracking value was available. The important change was the relationship between assignment and use, rather than the mere existence of an assignment somewhere in the file.

The repair also added a separate safeguard after generation. A sed operation mechanically overwrote tracking tags in the generated content. The incident record explains why that additional step was included: even when the prompt specified a tag, the generation step could omit it or write a different ID.

These were distinct checks at different points in the process. Moving the assignment supplied the intended value to the prompt. Correcting the generated links addressed the content that came back from generation.

The replacement targeted URL delimiters:

?tag=
&tag=

That distinction kept the operation from damaging an unrelated attribute such as data-tag="". The safeguard concerned tracking parameters in links, so the matching boundary mattered.

A further guard stopped publication with exit 1 if TRACKING_ID was empty. The desired behavior was explicit: an empty tracking value should prevent another post from being published.

The record recommends set -u as a general lesson for catching undefined references. It separately documents moving the assignment, correcting generated tags, and refusing publication with an empty ID as the implemented repair. Those details should not be collapsed into a claim that enabling set -u alone was the fix.

Together, the changes followed the value through assignment, prompt construction, generated content, and the decision to publish.

The database repair still left visitors seeing old links

Repairing existing articles exposed another gap between a successful operation and the result delivered to readers.

After the repair command ran, the database count of articles with empty tags fell from 152 to zero. The tagged count rose from 98 to 250, an increase matching the repaired set.

Yet an ordinary request to a published page still returned an empty tag. A request with a cache-bypassing query returned a populated tracking value. The recorded ordinary-request result was:

curl <URL>                 → &#038;tag=              ← 空

The final annotation means “empty.” The response still contained no tracking ID after tag=.

The recorded cause was page caching. The database had been updated, but sites holding cached HTML continued serving the earlier content. The storage repair and the visitor-facing repair were therefore not yet the same result.

The affected setup included WP Super Cache and WP Fastest Cache. For WP Super Cache, wp super-cache flush was unavailable as an unregistered command, so the cached files under wp-content/cache/supercache were removed directly. For WP Fastest Cache, the record likewise describes removing page-cache files because a WP-CLI command was unavailable.

The record also distinguishes this from wp cache flush: that command cleared the object cache, not the page cache involved in this incident.

After the page caches were removed, ordinary requests were used to verify populated tracking tags on four sites. That final check examined the HTML delivered without the cache-bypassing query.

The query-based check had been useful for showing the repaired content, but it had not established what an ordinary visitor was receiving. The ordinary request was the check that exposed the remaining failure.

(Source: our own measurement, 2026-09-12)

Lesson: verify where the value is used and what the reader receives

This incident had two versions of the same verification mistake. An assignment existed, but its value did not reach the prompt. A database repair succeeded, but the updated content did not immediately reach the reader.

For a similar shell script, start at the expansion that needs the value. Locate the assignment and confirm that it runs beforehand. Then inspect whether the assigned variable is actually used afterward. In this case, that review would have exposed a tracking assignment with no later consumer.

Check the generated link itself as well. A prompt containing the intended instruction is not the final article. The recorded safeguard operated after generation because the output could still omit or change the tracking ID.

Treat publication logs according to what they demonstrate. A success message for posting establishes that posting succeeded. To establish that a tracking parameter is populated, inspect the parameter in the published content.

For repairs to existing pages, repeat that inspection through an ordinary request after clearing the relevant page cache. A corrected database and a cache-bypassing response were useful intermediate evidence here, but neither proved that the usual response had changed.

The practical test is concrete: follow the tracking value from its assignment to its use, then inspect the link that the reader actually receives.

Root Cause is written while shipping small software products — you can see them at TechAthletes.

All posts →

Get new posts by email

We email you only when a new post goes up here. You can unsubscribe at any time.

Privacy policy