Jan 15, 2015 Tags: Bash, Linux

Looking for Wisdom about Bash

Ongoing attempt to collect knowledge and “good” links about the Linux Bash. Will be updated as needed.

Updated on Feb 12, 2016

See also: About Linux:

Navigate this page:

Bash Solutions

  • Make sure, the shell is Bash:

    test "$SHELL" = "/bin/bash" && echo true
    
  • Dmitry Dulepov: Set environment variable when “cd”ing to the directory

    set_enviroment_variable_per_dir_in_bash.sh:

    # Note: this is just a fragment that you should put in your ~/.bash_profile
    # Tested on Mac only!
    #
    # If you want a more sophisticated and feature rich solution here are some pointers:
    # - https://github.com/cxreg/smartcd
    # - http://swapoff.org/ondir.html
    
    function cd_ {
            if [ -e .env_off ] ; then
                    . .env_off
            fi
            builtin cd "$*"
            if [ -e .env ] ; then
                    . .env
            fi
    }
    
    alias cd=cd_
    
  • @nixcraft@Twitter:

    Problematic shell code:

    cd dir
    rm -r *.py
    

    Good code:

    cd dir || exit
    rm -r *.py
    

    Even better:

    [ -d "dir" ] && rm -r *.py
    

Solutions

List parts of $PATH

Short version:

echo $PATH | tr ":" "\n"

Long version:

#!/usr/bin/env bash
arr=$(echo $PATH | tr ";" "\n")
for part in $arr
do
   echo "$part"
done

Comments

comments powered by Disqus

Previous topic

Looking for Wisdom about Arduino

Next topic

Looking for Wisdom about CSS

Tags

Archives

Languages

Recent Posts

This Page