Bash 的隐藏功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/211378/
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
Hidden features of Bash
提问by Patrick
Shell scripts are often used as glue, for automation and simple one-off tasks. What are some of your favorite "hidden" features of the Bash shell/scripting language?
Shell 脚本通常用作粘合剂,用于自动化和简单的一次性任务。您最喜欢的 Bash shell/脚本语言有哪些“隐藏”功能?
- One feature per answer
- Give an example and short description of the feature, not just a link to documentation
- Label the feature using bold title as the first line
- 每个答案一个功能
- 给出该功能的示例和简短描述,而不仅仅是文档的链接
- 使用粗体标题作为第一行标记要素
See also:
也可以看看:
回答by chillitom
insert preceding line's final parameter
插入前一行的最后一个参数
alt-.the most useful key combination ever, try it and see, for some reason no one knows about this one.
alt-.有史以来最有用的组合键,试试看,不知为何没人知道这个组合。
press it again and again to select older last parameters.
一次又一次地按下它以选择较旧的最后参数。
great when you want to do something else to something you used just a moment ago.
当您想对刚才使用的东西做其他事情时,这很棒。
回答by Alex Reynolds
If you want to keep a process running after you log out:
如果要在注销后保持进程运行:
disown -h <pid>
disown -h <pid>
is a useful bash built-in. Unlike nohup
, you can run disown
on an already-running process.
是一个有用的内置 bash。与 不同nohup
,您可以disown
在已经运行的进程上运行。
First, stop your job with control-Z, get the pid from ps
(or use echo $!
), use bg
to send it to the background, then use disown
with the -h flag.
首先,使用 control-Z 停止您的工作,从ps
(或使用echo $!
)获取 pid ,使用bg
将其发送到后台,然后disown
与 -h 标志一起使用。
Don't forget to background your job or it will be killed when you logout.
不要忘记背景你的工作,否则当你注销时它会被杀死。
回答by Vinko Vrsalovic
Almost everything listed under EXPANSION section in the manual
手册中扩展部分下列出的几乎所有内容
In particular, parameter expansion:
特别是参数扩展:
$ I=foobar
$ echo ${I/oo/aa} #replacement
faabar
$ echo ${I:1:2} #substring
oo
$ echo ${I%bar} #trailing substitution
foo
$ echo ${I#foo} #leading substitution
bar
回答by GloryFish
My favorite:
我最喜欢的:
sudo !!
Rerun the previous command with sudo.
使用 sudo 重新运行上一个命令。
回答by Adam Liss
More magic key combinations:
更多魔法组合键:
Ctrl+ rbegins a “reverse incremental search” through your command history. As you continue to type, it retrieves the most recent command that contains all the text you enter.
Tabcompletes the word you've typed so far if it's unambiguous.
TabTablists all completions for the word you've typed so far.
Alt+ *insertsall possible completions, which is particularly helpful, say, if you've just entered a potentially destructive command with wildcards:
rm -r source/d*.c
Alt+ *rm -r source/delete_me.c source/do_not_delete_me.c
Ctrl+ Alt+ eperforms alias, history, and shell expansion on the current line. In other words, the current line is redisplayed as it will be processed by the shell:
ls $HOME/tmp
CtrlAlt+ els -N --color=tty -T 0 /home/cramey
Ctrl+r通过您的命令历史开始“反向增量搜索”。随着您继续键入,它会检索包含您输入的所有文本的最新命令。
Tab如果它是明确的,则完成您到目前为止输入的单词。
TabTab列出您到目前为止输入的单词的所有完成。
Alt+*插入所有可能的补全,这特别有用,例如,如果您刚刚输入了带有通配符的潜在破坏性命令:
rm -r source/d*.c
Alt+ *rm -r source/delete_me.c source/do_not_delete_me.c
Ctrl+ Alt+e在当前行上执行别名、历史和外壳扩展。换句话说,当前行被重新显示,因为它将被 shell 处理:
ls $HOME/tmp
CtrlAlt+ els -N --color=tty -T 0 /home/cramey
回答by Jaime Soriano
Get back history commands and arguments
取回历史命令和参数
It's possible to selectively access previous commands and arguments using the !
operator. It's very useful when you are working with long paths.
可以使用!
运算符有选择地访问以前的命令和参数。当您处理长路径时,它非常有用。
You can check your last commands with history
.
您可以使用history
.
You can use previous commands with !<n>
being n
the index of the command in history
, negative numbers count backwards from the last command in history.
您可以使用以前的命令!<n>
作为 中命令n
的索引history
,负数从历史记录中的最后一个命令向后计数。
ls -l foo bar
touch foo bar
!-2
You can use previous arguments with !:<n>
, zero is the command, >= 1 are the arguments.
您可以使用前面的参数!:<n>
,零是命令,>= 1 是参数。
ls -l foo
touch !:2
cp !:1 bar
And you can combine both with !<n>:<m>
你可以将两者结合起来 !<n>:<m>
touch foo bar
ls -l !:1 !:2
rm !-2:1 !-2:2
!-2
You can also use argument ranges !<n>:<x>-<y>
您还可以使用参数范围 !<n>:<x>-<y>
touch boo far
ls -l !:1-2
Other !
special modifiers are:
其他!
特殊修饰符是:
*
for all the argumentsls -l foo bar ls !*
^
for the first argument (!:1
==!^
)$
for the last argumentls -l foo bar cat !$ > /dev/null
*
对于所有的论点ls -l foo bar ls !*
^
对于第一个参数 (!:1
==!^
)$
对于最后一个参数ls -l foo bar cat !$ > /dev/null
回答by stephanea
I like the -x feature, allowing to see what's going on in your script.
我喜欢 -x 功能,它允许查看脚本中发生的事情。
bash -x script.sh
回答by Alberto Zaccagni
SECONDS=0; sleep 5 ; echo "that took approximately $SECONDS seconds"
SECONDS=0; sleep 5 ; echo "that took approximately $SECONDS seconds"
SECONDS
Each time this parameter is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.
秒
每次引用此参数时,都会返回自调用 shell 以来的秒数。如果将值分配给 SECONDS,则后续引用返回的值是自分配以来的秒数加上分配的值。如果 SECONDS 未设置,它将失去其特殊属性,即使随后被重置。
回答by danieljimenez
Here is one of my favorites. This sets tab completion to not be case sensitive. It's really great for quickly typing directory paths, especially on a Mac where the file system is not case sensitive by default. I put this in .inputrc
in my home folder.
这是我的最爱之一。这将选项卡完成设置为不区分大小写。它非常适合快速输入目录路径,尤其是在默认情况下文件系统不区分大小写的 Mac 上。我把它放在.inputrc
我的主文件夹中。
set completion-ignore-case on
回答by Vinko Vrsalovic
The special variable random:
特殊变量随机:
if [[ $(($RANDOM % 6)) = 0 ]]
then echo "BANG"
else
echo "Try again"
fi