Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

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.

like image 371
paratrooper Avatar asked Jun 21 '26 18:06

paratrooper


1 Answers

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

like image 58
William Pursell Avatar answered Jun 23 '26 14:06

William Pursell