Score:-2

Clear the entire console EXCEPT the current line with the "clear" command

bt flag

The need to clear terminal but save the current line with the command. In the script! (The script is called by the command. It is necessary to clear the entire terminal BEFORE this command, which calls the script.) Restoring a command from history is not suitable

Expected usage example:

  1. A script is being created "test.sh "
  2. We write some modified "clear" command into it:
    #!/usr/bin/env bash
    clear
    echo some text
    
  3. Apply

Current terminal state:

enter image description here

Current result after run 'test.sh':

enter image description here

What is the problem? The fact that the line with the command has disappeared + vscode does not display the left checkmark with the script error code.

Expected result:

enter image description here

How to do it?

P.S.:

The problem is not only in vscode, in other terminals the command is also completely erased

enter image description here

Avraam avatar
bt flag
It makes sense. The script is called by the command. It is necessary to clear the entire terminal BEFORE this command, which calls the script.
Will avatar
id flag
I agree with @Artur Meinild - I don’t think it’s very obviouss what you’re asking. I *think* what you’re trying to do is write a script that is called by writing ‘clear’, and that will clear everything from the terminal except the command you typed before ‘clear’ - is that right?
Artur Meinild avatar
vn flag
The solution to what you want is then *not* to include `clear` in the script. Also, this is beginning to look more like a VSCode issue, than a normal scripting issue.
Avraam avatar
bt flag
I don't include a `clear` - then I have trash that I want to get rid of. So - this action is not a solution
Avraam avatar
bt flag
1) In the context of the case - garbage/trash/history - doesnt matter 2) Hands/manually - bad way. The machine has to do it. Don't offer me such options, please.
Score:2
jp flag

One way

If you need a real prompt, you can use some cursor manipulation like this:

#!/bin/bash

# Save cursor position
printf '\033[s'
# Move the cursor up 1 line
printf '\033[1A'
# Clear the screen
printf '\033[1J'
# Restore cursor position
printf '\033[u'


echo "new thing"

If the above example needs more tweaking, please see for example ANSI escape sequences:Cursor Movement and also ANSI Escape Sequences for more of those.

Another way

You can still use clear if you want to, and then redraw your prompt afterwards like this:

#!/bin/bash

# Clear the screen
clear
# Build the prompt as it was before "clear"
line="$(printf '%s' "$USER@$HOSTNAME:$PWD$ $0")"
# For "root" prompt i.e. "#", comment out the above line and uncomment the below line
# line="$(printf '%s' "$USER@$HOSTNAME:$PWD# $0")"
# Print the prompt
echo "${line/\/home\/$USER/\~}"


echo "new thing"

Or even like this:

#!/bin/bash    

clear
line="$USER@$HOSTNAME:$PWD$ "
read -t 0.01 -ei "$(echo "$0")" -p "${line/\/home\/$USER/\~}"
echo


echo "new thing"

Appearances are deceiving, but appearances might be what you want ... It's not a real prompt though.

Artur Meinild avatar
vn flag
I think there was some VSCode specific issue here - but good suggestions.
Raffa avatar
jp flag
@ArturMeinild I tried to keep my answer purely `bash` focused … However, I honestly don’t use VSCode that much and don’t recall if it is supposed to clear its integrated terminal screen or not or even if it’s any different than the terminal emulator in Ubuntu.
Artur Meinild avatar
vn flag
Neither do I, so I might delete my answer - yours is definitely more on point, since the question was revised.
Raffa avatar
jp flag
@ArturMeinild Yeah I agree we don’t see much like this question … Solutions are a bit tricky and AFAIK no one single tool can do that … My first go to was `tput` but I couldn’t find a direct simple way with it to clear the lines above the cursor so I used escape sequences instead.
Avraam avatar
bt flag
The problem is not only in vscode, in other terminals the command is also completely erased - see added new P.S. block in the issue
Artur Meinild avatar
vn flag
@Avraam you still haven't explained the purpose of seeing the command you just ran after clearing the screen in a normal terminal. But I'll probably never understand it anyway, so let's just leave it at that.
Avraam avatar
bt flag
@ArturMeinild - 1) If a line with a command disappears, you cannot view the status of the utility in vs code - successful, error, warning. 2) The utility is voluminous and with interactive input, I want the user to focus on the content, while not squatting with writing clear or ctrl+l first and then calling the utility, but immediately getting a clean screen. But at the same time, the text of the command that the utility was called was saved.
Avraam avatar
bt flag
This is a very necessary functionality - to clean everything except the last command, and most importantly it should be by default, it's strange that there is no such thing.
Avraam avatar
bt flag
I tried out the ideas - they are not bad, closer to the goal, but still not that. The line with the command should not be touched at all.
Score:1
vn flag

Your question is not very clear (which ironically is the command you want to use), but I'll try to answer anyway.

It is my understanding that you call a script with a command (let's say it's called ./myscript), and want to clear the screen before the script is run.

You can do this in 2 ways.

  1. Clear the screen and then run the script:

    $ clear; ./myscript
    
  2. Have the clear command as the first line in your script:

     #!/bin/bash
     # Beginning of ./myscript
     clear
    
     # The rest of the script goes here
    

    And then just run the script, which will clear the screen, and then proceed to do other things:

     $ ./myscript
    

Finally, if you want to create an alias that clears the screen, and then reruns the last command, you can define:

alias clrun='clear; $(fc -ln -1)'

Then running clrun will do exactly this: Clear the screen and then rerun the previous command.

If this doesn't answer your question, you have to be much more precise in what you want.

Avraam avatar
bt flag
Unfortunately, your script doesn't do what i need. Added an example of input->output to a request with pictures.
Artur Meinild avatar
vn flag
Seems like problem with VSCode and/or your workflow. I don't have any solutions to that.
Avraam avatar
bt flag
I couldn't figure it out too, I hope someone else can
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.