Bash scripting tricks

Bash tricks

  1. Check bash version in script
    if ((BASH_VERSINFO[0] < 4))
    then
     echo "Sorry, you need at least bash-4.0 to run this script."
     exit 1
    fi

     

  2. Reading columns from a file in bash array
    table_collection=( $(cut -d $'\t' -f1 table_data.dat) )

    The above reads column 1 (f1 – field 1) in table_collection array

  3. Compress individual files in a directory
    for i in *; do tar -czvf $i.tar.gz $i; done

     

  4. Forward declaration in bash or shell script
    #!/bin/bash
    main() {
        foo
        bar
        baz
    }
    foo() {
    }
    bar() {
    }
    baz() {
    }
    main "$@"
    

    https://stackoverflow.com/questions/13588457/forward-function-declarations-in-a-bash-or-a-shell-script

  5. Let screen load your .bash_profile
    Edit ~/.screenrc to contain

    shell -$SHELL

    this will make screen start-up using a login shell which will load your ~/.bash_profile
    https://superuser.com/questions/343541/how-to-force-gnu-screen-to-load-my-bash-profile

  6. Coming soon…

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *