未加星标

Fixing your last bash command

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Guy I know - Oliver - command line ninja . Never makes a mistake. Can configure an AWS in a single long bash command. Typing speed through the roof. Bet you know someone like that too.

We mere mortals make mistakes and, while it's always good to learn from your mistakes, the first thing you have to do is fix them .

And to fix them you need to learn how to fix them.

Simple replace

Say you've typed an impossibly long command into the terminal with one irritating mistake. For me, it's usually something to do with xargs or curl

curl -s -I -X POST https://en.wikipedia.org/wiki/CURL | grep HTTP | cut -d ' ' -f 2

Not the greatest command - but say I couldn't spell wikipedia...

curl -s -I -X POST https://en.wikpedia.org/wiki/CURL | grep HTTP | cut -d ' ' -f 2 First solution: up and left

Naive, and effective. Press up to show the last command, keep tapping left until you get the the bit of the command you need to change, backspace to remove what you don't need and then enter what you do need

Second solution: bash vi mode

Bash has a vi mode, which can be activated by adding the following to your .bashrc .

set -o vi

If you're comfortable with vi you can now hit Escape to bounce into normal mode, Ctrl-P to go back to the last command, b a few times to get to the word you need to change... etc.

Vi mode is great - if you know a bit of vi. But you might not. So...

Third solution: quick substitution

How about something a little smarter:

^wikpedia^wikipedia

This is the bash quick substitution history expansion command - it runs the last command, substituting the first instance of the charaters after the first caret with the characters after the second caret.

Pretty neat huh? But that will olny work for the first instance - what if we need to replace every instance of wikpedia in the last command?

Fourth solution: full history substituton

Bash uses the ! character as the history expansion character - it is used to substitute a part of your current command with a previously executed command. One ! does nothing - but the previous command can be accessed with the !! sequence. So, to print out the last command, try:

echo !!

These history expansions can also take modifier options to change the string before it gets inserted. The syntax is <select>:<modifier> . For instance, to put the last command in quotes:

echo !!:q

And to perform a global substitution on it:

echo !!:gs/wikpedia/wikipedia

There is lots that can be done with the above syntax - just take a look at the documentation.

Fifth solution: retype the command

Seriously, by the time you've remembered how to do some of the above, wouldn't it have just been easier to type it out again.

Just don't mess it up this time, right?

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: CU
分页:12
转载请注明
本文标题:Fixing your last bash command
本站链接:http://www.codesec.net/view/482938.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(29)