Skip to content

Instantly share code, notes, and snippets.

@DrSpeedy
Last active January 30, 2020 20:48
Show Gist options
  • Save DrSpeedy/2d86e0e07a03c64eed4a5239e5148022 to your computer and use it in GitHub Desktop.
Save DrSpeedy/2d86e0e07a03c64eed4a5239e5148022 to your computer and use it in GitHub Desktop.

BASH CHEAT SHEET

Text processing

# Read file without empty lines and new line comments with grep
grep -v '^$\|^\s*\#' /path/to/file.txt
# Read file without empty lines, or any comments even on shared lines
sed '/^[[:blank:]]*#/d;s/#.*//;/^$/d' /path/to/file.txt

Utilities

# Find file name/pattern recusivly through all sub directories
f() { find $2 -name "*$1*"; }
# $ f <Some Pattern> <Path>
# Get full path to the runtime directory, export so the variable
# can be used by child processes
export script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Read simple config file, parse for only the text we need
# (no whitespace, comments..) and exports the values for our script
# and it's child-processes
# Example config:
# config_dir=${some_other_var}/bsdasd
# var_three=3

_config_raw=$(sed '/^[[:blank:]]*#/d;s/#.*//;/^$/d' ${script_dir}/config)
while read -r _cvar; do
    export "$(eval echo $_cvar)"
done <<< "$_config_raw"

RC

# Parse i3 keybindings and format them for the user
I3_CONFIG=~/.config/i3
PAGER='less'

alias i3cs='egrep ^bind ${I3_CONFIG}/config | cut -d '\'' '\'' -f 2- \
            | sed '\''s/ /\t/'\'' | column -ts $'\''\t'\'' | pr -2 -w 145 -t | $PAGER'
# Helpers for working with Laravel Homestead
alias art="php artisan"

alias gulp="node node_modules/gulp/bin/gulp.js"

function hms() {
    pushd "$HOME/Homestead" > /dev/null
    vagrant "${@:1}"
    popd > /dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment