help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How Bash echo line start from previous line


From: Andreas Kusalananda Kähäri
Subject: Re: How Bash echo line start from previous line
Date: Mon, 21 Aug 2023 13:14:39 +0200

On Mon, Aug 21, 2023 at 05:24:51PM +0700, Budi wrote:
> How can Bash echo line start from previous line above created by a
> simple echo i.e. not echo -n

I'm assuming you mean "How can I make the output of echo start at the
end of the string outputted on the previous line by some previous
invocation of echo?"

You can't.  The output of echo is always terminated by a newline (when
not using -n).  You can't make the output of echo start at the end of
the previous line without keeping track of the output of the previous
echo invocation somehow.

Personally, if I needed to do this, I would use not actually output
anything until I had collected the text I actually wanted to present to
the user.

        message=(
                "1st line"
                "2nd line"
                "3rd line"
        )
        message[0]+=" (1st line continued)"
        printf '%s\n' "${message[@]}"

You could obviously use some terminal control codes to move the cursor
up one line and then output the next line, but that would be a bit of a
hack.  It would also not work if the user had redirected the output of
the script to a file, and you would have to know the width of the output
on that line to be able to move the cursor to the correct position.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]