Bash Here Document未加引号分隔符时ASCII 1丢失问题咨询
Let's break down exactly what's happening here:
Core Difference Between Quoted vs Unquoted Here Documents
Bash treats here documents differently based on whether you quote the delimiter:
- Quoted delimiter (
<< "EOF"): The entire content inside the here document is treated as raw, literal text. No parameter expansion, command substitution, or special control character processing happens—every byte you write is preserved exactly as-is. - Unquoted delimiter (
<< EOF): Bash processes the content, including expanding variables, running command substitutions, and applying a little-known rule for control characters at the start of lines.
The Specific Reason Your ^A Vanishes
Looking at your xxd output, after the << EOF and newline, the next byte is 01 (your ^A)—meaning your here document line starts with ASCII 1 (SOH).
In unquoted here documents, Bash has a special behavior for lines that start with the ASCII 1 character:
It removes the leading ASCII 1 character entirely, and then treats the rest of the line as literal text (no further expansion).
That's exactly what's happening here—Bash sees the leading ^A, strips it out, and leaves the ^B (02) and ^C (03) intact, along with the trailing newline.
Why Quoting the Delimiter Fixes It
When you quote the delimiter (<< "EOF"), you're telling Bash to skip all processing of the here document content. Every byte—including the leading ASCII 1—is passed through directly to cat without modification, so your output retains all three control characters.
内容的提问来源于stack exchange,提问作者J B

