[Whonix-devel] Why read exits non-zero? How to safely use bash's built-in read command?

Patrick Schleizer patrick-mailinglists at whonix.org
Mon Oct 7 08:47:00 CEST 2019


I found a file that makes bash's built-in read command exit non-zero.

wget
https://raw.githubusercontent.com/Whonix/anon-connection-wizard/master/usr/share/anon-connection-wizard/advancedsettings.ico

########################################

#!/bin/bash

set -e

test -f ./advancedsettings.ico

while read -r line; do
   echo "$line"
   read -r first_word _ <<< "$line"
   echo "$first_word"
done < "./advancedsettings.ico"

########################################

#!/bin/bash

set -e

test -f ./advancedsettings.ico

while IFS= read -r -d '' line; do
   echo "$line"
   IFS= read -r -d '' first_word _ <<< "$line"
   echo "$first_word"
done < "./advancedsettings.ico"

########################################

bash -x ./a ; echo $?
+ set -e
+ test -f ./advancedsettings.ico
+ IFS=
+ read -r -d '' line
+ echo ''

+ IFS=
+ read -r -d '' first_word _
1

########################################

With a different - plain text - advancedsettings.ico file for testing
purposes the script would not fail. Some contents of
advancedsettings.ico is causing bash's built-in read command to exit
non-zero looks like.

Why does bash bash's built-in read command exit non-zero?

What is wrong about my usage of read

read -r first_word _ <<< "$line"

or

IFS= read -r -d '' first_word _ <<< "$line"

?

How to safely use bash's built-in read command?

Cheers,
Patrick


More information about the Whonix-devel mailing list