Google

2005-07-28

Bash Tips

Below are some useful Bash tips (For more check out Tip Of The week from Fedora News & Power Shell Usage Page):

There are a couple of tricks you can use to save time in a BASH terminal, by making use of BASH's history, silently stored in the file ~/.bash_history. These work in all BASH terminals, including Konsole, XTerm and gnome-terminal.

Here are a couple of Tips:

  • If you press the up, or down arrow, then BASH will cycle through the list of recently used commands
  • If you type part of a command, and then press , BASH will finish the command name for you. If there is more then one command, all of them are displayed on pressing again (see Command 2 in screenshot)
  • If you type part of a filename, and then press , then BASH will finish the filename for you. Again, if there is more than one option, all are diplayed on pressing again (see Command 3 in screenshot)
  • Press ctrl-R. A search box comes up for you to find previous commands
  • ! commands - This will search the History and execute a command. The command will be displayed before execution (See Commands 4-6). There are 5 ways you can use the ! in BASH
  1. !! - Execute The Last Command
  2. !x - Execute The Last Command Starting with x
  3. !-n - Execute The nth from last Command
  4. !?abc - Execute The last Command that contained abc
  5. !n - Execute The nth Command in history (not very useful)
  • ctrl+u and ctrl+k 'cut' from the cursor to the beginning and the end of the line respectively. This can later be pasted using ctrl+y
  • ctrl+w deletes a 'word' to the left of the cursor
  • If you press alt+. then the last parameter you passed to any command will appear at the cursor. Example below: First time I press alt+., it displayed -lr, 2nd time I did it it displayed Desktop etc.
  • [adil@ahlinux ~]$ ls -l
    [adil@ahlinux ~]$ cd Desktop
    [adil@ahlinux Desktop]$ ls -lr
    [adil@ahlinux Desktop]$ -lr Desktop -l

  • Define functions providing default options in .bashrc
  • function ls
    {
    command ls -F "$@"
    }
  • command runs the real command
  • "$@" inserts the user arguments

  • Commands with arguments can have different names:
    function duff
    {
    diff -ur "$@"
    }
  • Safe to export, for use in shells embedded in editors:
    export -f duff
  • Leaves the original name alone for programs relying on it
Post a Comment