删除在bash回声线 [英] Deleting echo line in bash

查看:87
本文介绍了删除在bash回声线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var=ab
echo -n "$var"

输出: AB

var=abc
echo "$var"

输出: ababc

我要删除第一个 AB 并替换 ABC

I want to delete the first ab and replace it by abc

我将如何做呢?

问候,INTELINSIDE

Regards, intelinside

推荐答案

也许你正在寻找的东西,如:

Perhaps you are looking for something like:

var=ab
echo -n "$var"
var=abc
echo -e "\r$var"

这实际上并没有删除任何东西,而仅仅是将光标移动到该行并覆盖的开始。如果被写入文字太短,previous写的内容依然可见。你可以写在旧的文本空间(很容易和便携式):

This doesn't actually delete anything, but merely moves the cursor to the beginning of the line and overwrites. If the text being written is too short, the content of the previous write will still be visible. You can either write spaces over the old text (very easy and portable):

printf "\r%-${COLUMNS}s" "$var"

或者使用一些终端转义序列来删除旧的文本(不便携式):

or use some terminal escape sequences to delete the old text (not portable):

echo -e "\r$var\033[K"

移动到行的开始,写新的文本,然后从光标删除到行的结尾。 (这可能不起作用,这取决于在终端上。)

to move to the beginning of the line, write new text, and then delete from the cursor to the end of the line. (This may not work, depending on the terminal.)

这篇关于删除在bash回声线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆