Today I want to share with you some of my favorite features of bash, I called them hidden because I’ve discovered that a lot of people don’t know or don’t use them, but to be honest they are not so hidden after all, they are in the
man page of bash, but how many of us have read it all ?
Thanks to xkcd.com for this explanation.
In short this is useful when you need root access for a command and you forget to use
The parameter “!!” is substituted with the last run command.
2) Playing with bash scripts:
bash -n bashscript.sh – To check the syntax w/o really executing the script
bash -x bashscript.sh – Run the script in debug mode, echoes the result each command, but in an abbreviated way.
3) Replace a string in the previous command.
You can use the syntax
^oldstring^newstring^ such as:
$ sudo apt-get install wrongpackagename $ ^wrongpackagename^rightpackagename^ sudo apt-get install rightpackagename
Or as alternative to get the same result you could use also:
$ sudo apt-get install wrongpackagename $ !!:s/wrongpackagename/rightpackagename sudo apt-get install rightpackagename
This syntax should be familiar to you if you use
4) Insert preceding line’s final parameter.
Sometimes you use a long parameter in a command and you need to use it again, for example you want to create a path and cd into it, you have 2 ways to do this:
# mkdir -p /this/is/mylong/pathto/some/directory # cd $_
$_ indicates exactly the last parameter of your last command, but perhaps you don’t want that parameter but another one, to navigate among the parameter you have used in your previous commands you can use the keyboard shortcut
Alt + . this will print the last parameter, press it again to select older last parameters and so on.
5) List only the subdirectory of the directory you are in.
Sometimes you just need to know the list of subdirectory and you don’t want to see all the files, to achieve this goal, you can run:
ls -d */
The ‘-d’ switch set ‘ls’ to show only directory entries, but if you give no parameter, it use the current directory as a parameter, and so it will show just “.” .
Ctrl + r begins 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. Much better than something like:
history |grep string
In case you miss the command you are searching giving a
Ctrl + r, you can reverse the search direction by hitting
CTRL + s
However,this is normally mapped to XOFF (interrupt data flow). Since that’s not too useful any more because we’re not using slow serial terminals, you can turn off that mapping with:
Ctrl + x + e Rapidly invoke the default editor (environment variable $EDITOR) to write a long, complex, or tricky command
Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). you’ll see the command you are writing in your editor, there you can change it, once you save and exit from that editor the modified command will be automatically run on your shell.
For more interesting commands i suggest to look at http://www.commandlinefu.com/
- Linux Terminal: An lsof Primer
- How to check if you are vulnerable to shellshock
- Ripping DVD with Handbrake on Linux
- Linux: Timeouting commands in shell scripts
- Switching to Linux, Checklist
Find me on Google+