Linux|系统管理|WEB开发

关注Linux,系统管理,WEB开发以及开源世界

Bash里的一些控制字符

| Comments

摘自Advanced Bash-Scripting Guide V6.0.05第部分第三章的特殊字符中的片断,并做了翻译。

  • Ctl-A 将光标移到行首
  • Ctl-B 退格键(但是我在ubuntu 9.10 bash 3.2.48上测试却是向前移动光标, 并不删除该字符)
  • Ctl-C 中断. 中断一个前台任务
  • Ctl-D 从当前shell退出(类似exit) EOF (end-of-file). 它也能中断在stdin的输入。
    当在一个终端(console)或者虚拟终端(比如xterm)里敲键盘时,ctl-D 会删除当前光标下的字符.如果当前没有任何字符了,则 Ctl-D 表示从当前会话退出,如果时xterm之类的窗口,则会关闭当前窗口。
  • Ctl-E 移动光标到行末
  • Ctl-F 光标朝前移动一个字符位置
  • Ctl-G 响铃(BEL).在一些老式的电传(teletype)终端里,它可能真的会导致电话铃响起,而在xterm之类的终端里,只是发出一个声响。
  • Ctl-H 相当于退格键

a="HH" # Two Ctl-H's -- backspaces

                      # ctl-V ctl-H, using vi/vim

echo "abcdef" # abcdef echo echo -n "abcdef$a " # abcd f

Space at end ^ ^ Backspaces twice.

echo echo -n "abcdef$a" # abcdef

No space at end ^ Doesn’t backspace (why?).

                      # Results may not be quite as expected.

echo; echo

Constantin Hagemeier suggests trying:

a=$’\010\010’

a=$’\b\b’

a=$’\x08\x08’

But, this does not change the results.

  • Ctl-I 水平tab键.
  • Ctl-J 新行 (换行符).脚本里,也可能当八进制’\012’或十六进制符号’\x0a’解释。
  • Ctl-K 垂直tab当在console或者xterm窗口里键入文本时, Ctl-K 表示删除从当前光标到行末的字符。在脚本里, Ctl-K 的行为表现就不同的,具体的可以看下面 Ctl-M 演示代码里Lee Lee Maschmeyer的例子。
  • Ctl-L ``换页 (终端清屏)。在终端里,其效果和clear命令类似(区别在于当前光标所在的行并不会清除) 。发送到打印机的话,Ctl-L 导致提前结束纸张。
  • Ctl-M ``回车.

read -n 1 -s -p \ $'Control-M leaves cursor at beginning of this line. Press Enter. \x0d'

       # Of course, '0d' is the hex equivalent of Control-M.

echo >&2 # The ‘-s’ makes anything typed silent,

       #+ so it is necessary to go to new line explicitly.

read -n 1 -s -p $’Control-J leaves cursor on next line. \x0a’

       #  '0a' is the hex equivalent of Control-J, linefeed.

echo >&2

#

read -n 1 -s -p $’And Control-K\x0bgoes straight down.’ echo >&2 # Control-K is vertical tab.

A better example of the effect of a vertical tab is:

var=$’\x0aThis is the bottom line\x0bThis is the top line\x0a’ echo "$var"

This works the same way as the above example. However:

echo "$var" | col

This causes the right end of the line to be higher than the left end.

It also explains why we started and ended with a line feed –

+ to avoid a garbled screen.

As Lee Maschmeyer explains:

————————–

In the [first vertical tab example] … the vertical tab

+ makes the printing go straight down without a carriage return.

This is true only on devices, such as the Linux console,

+ that can’t go "backward."

The real purpose of VT is to go straight UP, not down.

It can be used to print superscripts on a printer.

The col utility can be used to emulate the proper behavior of VT.

exit 0

Comments