Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Somehow I'm not able to read the trailing \n sign into the REPLY variable. Under any circumstances I want to avoid a blank line that results from the \n being echoed by read but echo one in case of another character. Given:

declare -l REPLY
read >&2 -r -N 1 -p "Acknowledged? (y):" REPLY
if [[ "$REPLY" != $'\n' ]]; then
  echo >&2

For me a possible workaround is it to make read to suppress (-s) echoing the input. But ideally the user should see the single character he input after the prompt.

Also IFS= read -d'' doesn't get me the \n character into the variable.

Any ideas?

share|improve this question

FWIW, your code works for me.

$ echo | bash -c 'read -rN1; echo "$BASH_VERSION <$REPLY>"'
4.2.36(1)-release <

With -N, no need to set IFS, and NL does end up in $REPLY. An empty $REPLY would only mean NUL (<Ctrl-Space>) or EOF or an error (exit status would be non-zero though for those two)

share|improve this answer

Read uses \n as its record separator, so it will never be in $REPLY. The newline you are seeing is as a result of echo. If you're wanting to check for only a newline on one line (ie. a blank line), one of these tests will work:

[[ $REPLY == "" ]] # true if line is empty
(( ${#REPLY} )) # true if line is not empty

Note that since you don't invoke read with IFS=, this will also match input that consists solely of whitespace.

share|improve this answer
I tried with IFS= and -d'' but no luck. – Tim Friske Nov 18 '12 at 14:27
@TimFriske You need to use -d '' rather than -d'', otherwise read will think you're passing no arguments to -d. What are you hoping to achieve by reading the actual newline, though? – Chris Down Nov 18 '12 at 14:38

As Chris mentioned, it's a separator, however to check the string length, you would do the following:

if [ -z "$REPLY" -o "$REPLY" == "y" ]; then
    echo "It was acknowledged"
    echo "Not acknowledged"
    exit 1
share|improve this answer
This will break if $REPLY contains any whitespace, it needs to be quoted appropriately. – Chris Down Nov 18 '12 at 14:42
Quote the variable; there's no need to quote y. – jw013 Nov 18 '12 at 16:49
It would also break for some values of $REPLY like = or '('... Don't use -o, use two [ commands separated by && and don't forget to quote your variables (and use = instead of ==). – Stéphane Chazelas Nov 19 '12 at 10:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.