bash 设置或更改光标的垂直位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14259311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:10:24  来源:igfitidea点击:

Set or change vertical position of the cursor

bash

提问by clx

As far as I know, it is possible to move the cursor to the left using the backspace sequence in an echo. But is there any possibility to change the vertical position of the cursor, using an echo?

据我所知,可以使用echo 中的退格序列将光标向左移动。但是是否有可能使用echo更改光标的垂直位置?

回答by miku

This section describes the ANSI escape sequences:

本节介绍 ANSI 转义序列:

Examples:

例子:

echo -en "3[s3[7B3[1;34m 7 lines down violet 3[u3[0m"
echo -en "3[s3[7A3[1;32m 7 lines up green 3[u3[0m"

And this section describes the tput utility:

本节描述了 tput 实用程序:

For a demonstration, see The floating clockin your terminal:

有关演示,请参阅终端中的浮动时钟

An example script taken from http://www.cyberciti.biz/tips/spice-up-your-unix-linux-shell-scripts.html:

取自http://www.cyberciti.biz/tips/spice-up-your-unix-linux-shell-scripts.html的示例脚本:

#!/bin/bash

tput clear      # clear the screen

tput cup 3 15   # Move cursor to screen location X,Y (top left is 0,0)

tput setaf 3    # Set a foreground colour using ANSI escape
echo "XYX Corp LTD."
tput sgr0

tput cup 5 17
tput rev        # Set reverse video mode
echo "M A I N - M E N U"
tput sgr0

tput cup 7 15; echo "1. User Management"
tput cup 8 15; echo "2. Service Management"
tput cup 9 15; echo "3. Process Management"
tput cup 10 15; echo "4. Backup"

tput bold       # Set bold mode 
tput cup 12 15
read -p "Enter your choice [1-4] " choice

tput clear
tput sgr0
tput rc

tput example

输入示例

回答by kmkaplan

Using echowill restrict you to a specific terminal type. It is better to use tput.

使用echo将限制您使用特定的终端类型。最好使用tput.

tput cup 10 4; echo there

will put cursor on row 10 column 4 and print “there” at that position.

将光标放在第 10 行第 4 列并在该位置打印“那里”。

For more elementary movements you have tput cub1to move left, tput cuf1to move right, tput cuu1to move up and tput cud1to move the cursor down.

对于更基本的移动,您必须tput cub1向左tput cuf1移动、向右tput cuu1移动、向上移动和tput cud1向下移动光标。

回答by mr.spuratic

Yes, as noted by miku, ANSI terminal escapes, or a little cleaner is tput which should work on any terminal:

是的,正如 miku 所指出的,ANSI 终端转义,或者更干净的 tput 应该适用于任何终端:

tput cuu 1             # move cursor up 1 position
nudgeup=`tput cuu 2`   # save it for later
nudgeleft=`tput cub 2`   
echo -n $nudgeup $nudgeleft

See a couple of sections after the ANSI section in the BASH prompt howto.

请参阅 BASH 提示 howto 中 ANSI 部分之后的几个部分。

tput also returns an exit code that tells you if something is not supported.

tput 还会返回一个退出代码,告诉您是否不支持某些内容。