mikewilliamson
mikewilliamson

Reputation: 24803

read: Illegal option -d

Here is the offending part of my script:

read -d '' TEXT <<'EOF'
Some Multiline
text that
I would like
in 
a 
var
EOF

echo "$TEXT" > ~/some/file.txt

and the error:

read: 175: Illegal option -d

I use this read -d all over the place and it works fine. Not sure why its not happy now. I'm running the script on Ubuntu 10.10

Fixes? Workarounds?

Upvotes: 19

Views: 21055

Answers (2)

paxdiablo
paxdiablo

Reputation: 882626

If you run sh and then try that command, you get:

read: 1: Illegal option -d

If you do it while still in bash, it works fine.

I therefore deduce that your script is not running under bash.

Make sure that your script begins with the line:

#!/usr/bin/env bash

(or equivalent) so that the correct shell is running the script.

Alternatively, if you cannot do that (because the script is not a bash one), just be aware that -d is a bash feature and may not be available in other shells. In that case, you will need to find another way.

Upvotes: 28

hobbs
hobbs

Reputation: 240679

The -d option to read is a feature unique to bash, not part of the POSIX standard (which only specifies -r and -p options to read). When you run your script with sh on Ubuntu, it's getting run with dash, which is a POSIX shell, and not bash. If you want the script to run under bash then you should run it with bash, or give it a #!/bin/bash shebang. Otherwise, it should be expected to run under any POSIX sh.

Upvotes: 5

Related Questions