你怎么写一个回声线?

时间:2011-09-08 02:31:53

标签: unix

我正在尝试更改终端中回显线的文本而\ r \ n适用于我,但不是当我将它放在.sh文件中时。我想运行.sh文件以1秒的间隔更改回显的行(不要问为什么)。当我做的时候

echo -ne 'hello\r'
sleep 1
echo -ne 'bye\r'

输出
-ne hello
-ne bye

我如何实现目标?

2 个答案:

答案 0 :(得分:4)

Cursor Movement

重要部分:

- Position the Cursor:
  \033[<L>;<C>H
     Or
  \033[<L>;<C>f
  puts the cursor at line L and column C.
- Move the cursor up N lines:
  \033[<N>A
- Move the cursor down N lines:
  \033[<N>B
- Move the cursor forward N columns:
  \033[<N>C
- Move the cursor backward N columns:
  \033[<N>D

- Clear the screen, move to (0,0):
  \033[2J
- Erase to end of line:
  \033[K

- Save cursor position:
  \033[s
- Restore cursor position:
  \033[u

如下所示:

echo -ne "\033[50D"
echo -ne "\033[K"

答案 1 :(得分:3)

使用标准化的printf,而不是-ne的{​​{1}}选项,而不是echo

printf '\033[K%s\r' "hello world"
sleep 1
printf '\033[K%s\r' "bye now"

确保在退出时打印换行符,例如与

trap 'echo' 0

在第一个printf命令之前。