How to show read prompt with a new line
You can separate the prompt from the actual read :
echo "Please input:"
read name
You can put both on a single line :
echo "Please input:" ; read name
You can also use a different form of quoting :
read -p $'Please input\n' name
This is barely shorter, and many would probably find it a bit less readable, but that is a matter of taste.
How do I add a line break for read command?
I like Huang F. Lei's answer, but if you don't like the literal line break, this works:
read -p "Please Enter a Message: `echo $'\n> '`" message
Shows:
Please Enter a Message:
> _
...where _
is where the cursor ends up. Note that since trailing newlines are usually dropped during command substitution, I've included the >
afterward. But actually, your original question doesn't seem to want that prompt bit, so:
# Get a carriage return into `cr` -- there *has* to be a better way to do this
cr=`echo $'\n.'`
cr=${cr%.}
# Use it
read -p "Please Enter a Message: $cr" message
Shows
Please Enter a Message:
_
There has to be a better way, though.
BASH: When reading user input, Enter brings new line
You want to move the cursor up one line. This is achieved with tput cuu1
:
echo -e "Enter name: \c"
read U_IP_NAME
tput cuu1
echo -e "Your said your name is : $U_IP_NAME"
More info with man tput
and man terminfo
.
Using read without triggering a newline action on terminal
The Enter that causes read
to return necessarily moves the cursor to the next line. You need to use terminal escapes to get it back to the previous line.
And the rest of your script has some problems anyway. Here's something that works, it should give you a better starting point:
#!/bin/bash -e
PROMPT=">"
while read -p "${PROMPT}" line; do
echo -en "\033[1A\033[2K"
echo "You typed: $line"
done
\033
is an Esc; the \033[1A
moves the cursor to the previous line, \033[2K
erases whatever was on it.
How to break a line (add a newline) in read -p in Bash?
You can do it this way:
read -p $'Please Enter the percent [30 between 100]\x0a The value is Default = 80 :' scale_percent
we use above syntax to insert hex values, we insert \x0a
which is the hexadecimal value of the newline character (LF). You can use same syntax above with echo
to produce new lines, eg:
echo $'One line\x0asecond line'
This is a feature of BASH 2, it is documented here, the $''
is used to translate all escape sequences inside it for its ascii transcription. So we could obtain the same result of example above, this way:
echo $'One line\nsecond line'
How to display \r\n as actual new line in command prompt?
The result is in bytes. So you have to call decode
method to convert the byte object to string object.
>>> print(output.decode("utf-8"))
R: read in a line from user input with newline character
Do you simply want to remove the extra \
? If so:
x
#> [1] "first\\nsecond"
gsub("\\n", "\n", x, fixed = TRUE)
#> [1] "first\nsecond"
Created on 2022-01-04 by the reprex package (v2.0.1)
How to partially read input until a new line using read()?
I'm trying to partially read input until a new line is inputed "\n". But how can I do that with the read() function?
The only way you can achieve this with a read
is to read one character at a time. Once you've read the '\n'
character, stop.
how do I check what is being read?
while (read(fd, buf + offset, 1) == 1) {
if (buf[offset] == '\n') break;
offset += 1;
}
Shell Script, read on same line after echoing a message
The shebang #!/bin/sh
means you're writing code for either the historical Bourne shell (still found on some systems like Solaris I think), or more likely, the standard shell language as defined by POSIX. This means that read -p
and echo -n
are both unreliable.
The standard/portable solution is:
printf 'Enter [y/n] : '
read -r opt
(The -r
prevents the special treatment of \
, since read
normally accepts that as a line-continuation when it's at the end of a line.)
If you know that your script will be run on systems that have Bash, you can change the shebang to #!/bin/bash
(or #!/usr/bin/env bash
) and use all the fancy Bash features. (Many systems have /bin/sh
symlinked to bash
so it works either way, but relying on that is bad practice, and bash
actually disables some of its own features when executed under the name sh
.)
Related Topics
Possibly Undefined MACro: Ac_Prog_Libtool
How to Prevent Matlab Printing False Space and Use Wrong Fonts
Split Delimited File into Smaller Files by Column
Generate Public Ssh Key from Private Key
Linux: Handling a Segmentation Fault and Getting a Core Dump
Is It Good Practice to Use Mkdir as File-Based Locking on Linux
Curl: (2) Failed Initialization
Sync Two Computers Going Through a Bridge with Rsync
Suppress or Prevent Duplicate Inotifywait Events
Padding Empty Field in Unix Join Operation
How to Access the Base Filename of a File You Are Sourcing in Bash
How to Cd into a Directory Using Perl
Best Text Search Engine for Integrating with Custom Web App
Run a Command Conditionally with Netcat and Grep
<Command Line>:1:1: Error: MACro Names Must Be Identifiers
Bash: How to Traverse Directory Structure and Execute Commands