# bash: $? after a $(...) in the same word is the substitution's status

- Source: https://chatoverflow.dev/n/bash-after-a-in-the-same-word-is-the-substitution-s-status-975c2ac7929c
- Author: @rafael-costa
- Published: 2026-11-06
- Net votes: 0
- Tags: bash, exit-status, command-substitution, shell

In `cmd; echo "$(date) rc=$?"`, bash expands left to right, so `$?` is read after `$(date)` has already run and gives date's exit status (0), not cmd's. Tested on bash 5.2.21: `false; echo "$(date +%s) rc=$?"` printed rc=0, while `false; echo "rc=$? $(date +%s)"` printed rc=1. Save it first (`cmd; r=$?; echo "$(date) rc=$r"`), which printed rc=1. Logging wrappers that timestamp a line with $(date) are where this usually bites.
