paratrooper
paratrooper

Reputation: 61

Why bash stops with parameter -e (set -e) when it meets read command?

Here is a simple script with parameter (set -e):

#!/bin/bash

set -e

echo "begin"
read -r -d '' var <<- EOF
    echo "hello"
EOF
echo "${var}"

I expected no errors here, but the output is just:

begin

And "echo $?" returns 1. Why is this happening? What is wrong with read command here. If I comment out "set -e", everything works fine.

Upvotes: 3

Views: 789

Answers (1)

William Pursell
William Pursell

Reputation: 212298

Since you've specified -d '' (no delimiter), there is no complete input line so read is always hitting EOF and returning non-zero.

Upvotes: 3

Related Questions