Sample Header Ad - 728x90

bash variable substitution inside heredoc delimiter: how does it work?

0 votes
0 answers
43 views
For a school project, I am tasked with making my own (simplified) shell, with bash being the reference point. This includes replicating heredoc behavior which was fun until I stumbled upon variable substitution INSIDE of the delimiter, as such:
TESTVAR=hello
followed by cat hello > $TESTVAR hello As demonstrated here, typing the value of the variable manually does not end the heredoc, whereas typing $TESTVAR does. Does this mean that variables are not substituted if they're inside of the delimiter? Disabling variable substitution results in the same thing: cat hello > $TESTVAR hello And getting wild with the quotes also results in the same thing: cat hello > TVAR > $TESTVAR hello TVAR All evidence seems to point towards $TESTVAR does not get substituted **because** it is part of a heredoc delimiter, but I can't find a definitive answer to this! Does anyone know?
Asked by Mika (101 rep)
May 9, 2025, 01:58 PM
Last activity: May 9, 2025, 02:17 PM