Bash Tips and Notes

Bash IFs

if [[ $1 ]]; then
	... code
else
	... code
fi


Even though single [ ] quotes do too in places, use double [[.

Bash auto complete

The following is for thyping cde <tab> and then bash completing with a matching directory from /etc. Press enter to cd into that dir.

Put these three functions in .bashrc

 

function cde(){
    cd /etc/$1
}
function _etcs() {
    local dirs=("/etc/$2"*)
    [[ -e ${dirs[0]} ]] && COMPREPLY=( "${dirs[@]##*/}" )
}
complete -F _etcs cde
Published
20-Oct-2022
Updated
29-Oct-2022