Unix script 16 - interactive shell

本文提供了使用 Unix 或 Linux shell 的一些实用提示,包括 bash 和 ksh 的历史搜索工具、快捷键使用方法等,帮助提高日常操作效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Here are a few quick hints for using the UNIX or Linux shell interactively. Personally Irecommend the bash shell for most interactive use; it is available on justabout every *nix flavour, and very pleasant to use as a login shell. However the root shell should always be /bin/sh, whether that points to bash or bourne shell.

bash

bash has some very handy history-searching tools; the up and down arrow keyswill scroll through the history of previous commands. More usefully, Ctrl+r will do a reverse-search, matching any part of the command line. Hit ESC and the selected command will be pasted into the current shell for you to edit as required.

If you want to repeat a command you ran before, and you know what characters it started with, you can do this:

bash$ ls /tmp
(list of files in /tmp)
bash$ touch /tmp/foo
bash$ !l
ls /tmp
(list of files in /tmp, now including /tmp/foo)

As well as the arrow keys, you can use PageUp and PageDn to navigate to the start and end of the command line.

ksh

You can make ksh more usable by adding history commands, either in vi or emacs mode. There are a number of ways to do this, depending on the exact circumstances. set -o vi, ksh -o vi, or exec ksh -o vi (where "vi" could be replaced by "emacs" if you prefer emacs mode).

If you want to start a ksh session from another interactive shell, you can just call ksh like this:

csh% # oh no, it's csh!
csh% ksh
ksh$ # phew, that's better
ksh$ # do some stuff under ksh
ksh$ # then leave it back at the csh prompt:
ksh$ exit
csh%

This will start a new ksh session, which you can exit from and return to the previous shell. Alternatively, you could replace the csh (or whatever shell) with a ksh shell, with the exec command:

csh% # oh no, it's csh!
csh% exec ksh
ksh$ # do some stuff under ksh
ksh$ exit

login:

The difference here is that you don't get the csh session back.

The good stuff is the history:

csh% ksh
ksh$ set -o vi
ksh$ # You can now edit the history with vi-like commands, 
  # and use ESC-k to access the history.

If you hit ESC then k, then by repeatedlyhitting k you scroll backwards through the command history.You can use vi command-mode and entry-mode commands to edit the commands, like this:

ksh$ touch foo
  ESC-k (enter vi mode, brings up the previous command)
  w (skip to the next word, to go from "touch" to "foo"
  cw (change word) bar (change "foo" to "bar")
ksh$ touch bar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值