Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH command prompt: add characters behind command

When editing PS1, one using escape sequences can manipulate the form of the prompt in almost anyway. Howver what holds true every time is that stdout starts right after the command input. For for simplicity's sake, here's an example of a simple prompt that adds some elements below the command entry:

PS1='aaaaaaaa\n\[\033[1B\]bbbbbbbb\n\[\033[2A\]\u@\h:\w\$ '

that looks more or less like this:

aaaaaaaa
user@hostname:~$ █
bbbbbbbb

Notice the position of the cursor once the drawing of the prompt is finished. The \033[ used in the PS1 variable manipulate the cursor (moving it up and down) to be able to draw the 'b' separator below the prompt and return to position.

If however a command is run, stdout as expected overwrites whatever is below the command:

aaaaaaaa
user@hostname:~$ echo 'hello '
hello bb
aaaaaaaa
user@hostname:~$ █
bbbbbbbb

The question is whether there is a way to manipulate the behavior of the prompt from the point of the command's last character in the following fachion:

<editable prompt><command><editable area after command?>

If for example I could instruct bash to print a newline or any escape sequence after every command that would solve the problem portrayed by the example.

like image 646
Stratos Papadoudis Avatar asked Mar 07 '26 21:03

Stratos Papadoudis


1 Answers

This trick might work. I got the idea from https://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command but I simplified as much as I could.

trap 'echo' DEBUG

The theory is simply to use bash's DEBUG trap to write a blank line before executing anything.

like image 131
Chris Lear Avatar answered Mar 10 '26 12:03

Chris Lear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!