Insert New Line to Bash Prompts

Insert new line to bash prompts

Change the PS1 Bash prompt variable:

PS1="\n$PS1"

How to insert a new line in Linux shell script?

The simplest way to insert a new line between echo statements is to insert an echo without arguments, for example:

echo Create the snapshots
echo
echo Snapshot created

That is, echo without any arguments will print a blank line.

Another alternative to use a single echo statement with the -e flag and embedded newline characters \n:

echo -e "Create the snapshots\n\nSnapshot created"

However, this is not portable, as the -e flag doesn't work consistently in all systems. A better way if you really want to do this is using printf:

printf "Create the snapshots\n\nSnapshot created\n"

This works more reliably in many systems, though it's not POSIX compliant. Notice that you must manually add a \n at the end, as printf doesn't append a newline automatically as echo does.

How to add new line after bash command before output

What about adding PROMPT_COMMAND=echo to your .bashrc? This will execute "echo" after each command.

Or you can use something like trap echo DEBUG to intercept the execution and strike that echo before each command.

This works for me:

bash-3.2$ cat .bashrc

trap echo DEBUG
bash-3.2$ cd foo/

bash-3.2$ ls

foo
bash-3.2$

Insert data in bash prompt with script

Change your main while-loop to read from a different file descriptor than stdin and use read to read from stdin and use -s to suppress text from showing up in console.

while read -u 3 line; do
# Your rest of the code
read -s -p "Enter password: " password
done 3<myfile

Bash (Mint): How to add a newline to a PS3 prompt?


$ PS3="Choose 1..10"$'\n'"0 to exit: "
$ select choice in $(seq 10)
> do
> [[ $choice -eq 0 ]] && break
> #do stuff for choice 1 to 10
> done
1) 1
2) 2
3) 3
4) 4
5) 5
6) 6
7) 7
8) 8
9) 9
10) 10
Choose 1..10
0 to exit: 0
$

Just an additional $'\n' is required wrt bash context, which is inserted in between the PS3 environment variable. It's similar to the way we set it for IFS

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.

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.

Trying to conditionally insert a newline in my ZSH prompt. Having trouble figuring it out

With a command substitution like $(...), trailing newlines are trimmed from the
replacement text, so the \n isn't included in the resulting prompt string.

Note that it's only the last newlines that are removed; newlines in the middle
are retained. So a simple fix would be to add some text (e.g. a space) after
the newline:

autoload -Uz vcs_info
setopt PROMPT_SUBST

PROMPT=$'$(ps1_head)[%F{32}%n@%m%f %~]%(!.#.$) '

function ps1_head() {
vcs_info
var=${vcs_info_msg_0_}
[ -n "$var" ] && printf '%s\n ' "$var"
}

For more formatting flexibility, it's often easier to use a zsh precmd:

my_precmd() {
vcs_info
[[ -n $vcs_info_msg_0_ ]] && print $vcs_info_msg_0_
}

autoload -Uz vcs_info
zstyle ':vcs_info:git:*' formats '(%s)-[%b]'
autoload -Uz add-zsh-hook
add-zsh-hook precmd my_precmd
PROMPT='[%F{32}%n@%m%f %~]%(!.#.$) '

The precmd will be executed prior to every prompt being displayed, and can print
information and set variables. It's frequently used to fill in the psvar array, which
is easy to use in a zsh prompt. The zstyle command can be used to format the
output from vcs_info.



Related Topics



Leave a reply



Submit