Skip to main content
Remove posix, move to comment
Source Link
Tom Hale
  • 33.3k
  • 42
  • 163
  • 257

shell bash: read: how to capture '\n' (newline) character?

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rd '' -n1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

I want the same method to be able to capture ^D or \004.

If read can't do it, what is the work around?

I'm interested in how to do this in both bash and POSIX sh.

shell: read: how to capture '\n' (newline) character?

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rd '' -n1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

I want the same method to be able to capture ^D or \004.

If read can't do it, what is the work around?

I'm interested in how to do this in both bash and POSIX sh.

bash: read: how to capture '\n' (newline) character?

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rd '' -n1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

I want the same method to be able to capture ^D or \004.

If read can't do it, what is the work around?

Add ability to capture '\004'
Source Link
Tom Hale
  • 33.3k
  • 42
  • 163
  • 257

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rnrd 1'' -n1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

I want the same method to be able to capture ^D or \004.

If read can't do it, what is the work around?

I'm interested in how to do this in both bash and POSIX sh.

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rn 1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

If read can't do it, what is the work around?

I'm interested in how to do this in both bash and POSIX sh.

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rd '' -n1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

I want the same method to be able to capture ^D or \004.

If read can't do it, what is the work around?

I'm interested in how to do this in both bash and POSIX sh.

Source Link
Tom Hale
  • 33.3k
  • 42
  • 163
  • 257

shell: read: how to capture '\n' (newline) character?

Can I use read to capture the \n \012 or newline character?

Define test function:

f() { read -rn 1 -p "Enter a character: " char &&
      printf "\nYou entered: %q\n" "$char"; }

Run the function, press Enter:

$ f;
Enter a character: 

You entered: ''

Hmmm. It's a null string.

How do I get my expected output:

$ f;
Enter a character:

You entered: $'\012'
$ 

If read can't do it, what is the work around?

I'm interested in how to do this in both bash and POSIX sh.