When does Bash read heredocs?

72 views Asked by At

I'm trying to understand the behavior of Bash when it comes to reading heredocs, as I am trying to implement a minishell based on bash in C. I am using GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18), but the following applies to newer versions too (5.x) as I tested them. Specifically, I'm interested in knowing the circumstances under which Bash reads the content of a here document (<<). I've encountered some scenarios where it seems Bash behaves unexpectedly, and I'd like to clarify these points.

When using a heredoc in a commad, how does Bash determine when to read the heredoc? For example:

|||||| cat <<END In this case, Bash reports a syntax error (bash: syntax error near unexpected token ||) and doesn't read the heredoc. Why does this happen? Conversely, in this other case, how does Bash handle it? For instance: cat <<END ||||||| Here, Bash reports same syntax error but still reads the heredoc from stdin. Why does this behavior differ from the previous example?

I'm seeking clarification on these behaviors to better understand how Bash processes heredocs in different contexts.

0

There are 0 answers