Aug 082012

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 ?

1) xkcdcom-149-2

Thanks to for this explanation.
In short this is useful when you need root access for a command and you forget to use sudo.
The parameter “!!” is substituted with the last run command.

2) Playing with bash scripts:

bash -n – To check the syntax w/o really executing the script
bash -x – 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 sed or vi

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 “.” .

Keyboard Shortcuts

6) 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:

stty -ixon

7) 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

Popular Posts:

Flattr this!

  9 Responses to “7 hidden features of bash”

  1. Nice post. In addition to your !! comment running the last command, you can also use something like !ls to run the last ls command you used, or !rmdir to run the last call to rmdir. It pattern matches too meaning you could just do !rmd and that would match the first command in your history starting with rmd, most likely rmdir.

  2. I didn’t know the Ctrl+x+e trick, really usefull, thanks!

  3. Wow very useful tricks! Thanks for sharing!

  4. “ls -d” is not a Bash feature. “ls” is an external command, “-d” is a feature of “ls”.

    “!$” also refers to the last word on the preceding command line.

    “!!:p” prints, but does not execute, the previous command.

    “fc” invokes your default editor to edit your previous command.

    “set -x” enables debugging (as “bash -x”), but for your current Bash instance. “set +x” disables it.

  5. Just a note about #5: that will only list non-hidden directories, just in case you were looking for .something directories.

  6. I forgot about ^search^replace^ – handy if I type “apt-cache install ” accidentally instead of “apt-get install …”,

    two other shortcuts I use are “cd -” to switch between the previously viewed directory,
    and “cd” on its own to go home.

  7. Always done
    Is there any reason we should be doing

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>