+ Reply to Thread
Results 1 to 4 of 4

Thread: bash hacks

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    bash hacks

    Here are 12 useful tricks for more effective bash shell use and programming:

    Hack #1: Generating a random number:

    The environment variable $RANDOM produces a random integer between 0 and (2^16)-1. You can use it in a shell script to randomly select a list of instructions to execute.


    Code:
    arr=([0]="Random string #1" [1]="Random string #2" [2]="Random string #3");
    declare -i randomString=$RANDOM%3;
    echo $randomString

    Hack #2: Toggling between directories:

    Use cd - to toggle between the current directory and the last one. The directory you switch to is printed to the terminal.


    Hack #3: pushd, popd, and dirs:

    Unix has a useful object called the directory stack, which provides a path to take through your filesystem.

    pushd directory adds directory to the top of the directory stack. When you pop the stack, bash brings you to the directory at the top of the stack.

    popd pops the directory stack, then displays the current directory plus the directory stack.

    dirs displays the directory stack as if you had just popped it.


    Hack #4: Using aliases:

    An alias is a shortcut, an abbreviated command. Aliases are very useful for executing repetitive commands very quickly. It is often useful to store aliases in a script, like this:

    Code:
    #!/bin/bash
    # This script creates aliases for annoyingly long and tedious cd commands.
    alias gotoscripts="cd ~/Desktop/SourceCodes/Scripts";
    alias gotoworkfiles="cd ~/Desktop/WorkFiles/Current";
    alias gotoruby="cd ~/Desktop/SourceCodes/Ruby";
    Typing the name of an alias executes whatever command its value is set to.

    You can delete an alias by typing unalias "myalias".


    Hack #5: CDPATH:

    $CDPATH is an environment variable that sets the default file path for directory changes. If you enter cd and then the name of a directory in $CDPATH, bash automatically brings you to that directory. Here's the syntax:

    Code:
    export CDPATH=/usr/local
    If you have a directory named bin in usr/local, typing cd bin will always bring you to /usr/local/bin, even if you are outside the containing directory.


    Hack #6: sort

    The command outputs the lines of a file sorted alphabetically. To sort a file, type sort filename at the prompt. You can then use I/O redirection to send the sorted output to another file.


    Hack #7: cal

    cal is a Unix program that prints a calendar of dates. The default is to display the current month, like this:

    Code:
    bash-2.3$ cal
       December 2009
    Su Mo Tu We Th Fr Sa
           1  2  3  4  5
     6  7  8  9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30 31
    There are several options for displaying the calendar in different formats. Here are two of them:

    cal -m Jul - display the calendar for July of this year.

    cal -y 2007 - display the calendar for all months in 2007.


    Hack #8: Path to a command:

    The type program shows the path to the binary for a command if it is a regular command, and prints "---- is a shell builtin" if the command is a builtin. This is useful for determining what to put in the she-bang line of a script:

    Code:
    bash-3.2$ type perl
    /usr/bin/perl
    The she-bang for perl scripts is then #!/usr/bin/perl.


    Hack #9: Formatting the date:

    There are several specifiers for formatting the date. These are used by typing a + and then a string in quotes with the specifiers in it. The shell will replace the format specifiers with the parts of the date.

    Example:
    Code:
    bash-3.2$ date +"%m/%d/%y"
    12/02/09
    Options:
    %m - month
    %d - day
    %y - abbreviated year
    %D - month-day-year
    %Y - full year
    %a - abbrevated day name
    %A - full day name
    %b - abbrevated month name
    %B - full month name


    Hack #10: Word count:

    wc counts the words, characters, or bytes in the given file. It's options are:

    -c - number of bytes
    -l - number of lines
    -m - number of characters (including multibyte characters)
    -w - number of words


    Hack #11: Command history:

    There are three useful hacks for using the command history of the terminal:

    history - this command prints the command history.

    HISTTIMEFORMAT is an environment variable that specifies the format for displaying the command history. There are a few formatting options for this variable.

    Code:
    export HISTTIMEFORMAT "%F %T"
    This displays the date and time for each command in the history.

    The command history can be searched by typing Ctrl+R and then typing a keyword.


    Hack #12: Listing open files:

    lsof lists all open files with information about each of them. This information includes process name, process ID, user, file descriptor, type (DIR or REG), device number, size, node number, and name of the file.


    Summary of environment variables:

    $CDPATH - the default file path
    $COLUMNS - the width of the terminal window, in characters
    $HISTTIMEFORMAT - the format for displaying the command history
    $LINES - the height of the terminal window, in characters
    $OLDPWD - previous directory
    $PATH - a list of paths to the currently running program
    $PWD - current directory
    $RANDOM - random number
    $SHELL - the path to the shell
    $TERM - the name of the terminal
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: bash hacks

    I didn't know about some of these things.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  4. #3
    Join Date
    Nov 2009
    Location
    London
    Posts
    866
    Blog Entries
    3
    Rep Power
    14

    Re: bash hacks

    Good easy to follow tutorial on bash, thanks

    +rep

  5. #4
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: bash hacks

    Things I did not know, nice, more rep (the little I have).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 09-20-2010, 08:46 PM
  2. Vim hacks
    By DarkLordoftheMonkeys in forum Linux Tutorials, Guides and Tips
    Replies: 1
    Last Post: 12-19-2009, 10:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts