Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sanusart/84abd99190d670040a41 to your computer and use it in GitHub Desktop.
Save sanusart/84abd99190d670040a41 to your computer and use it in GitHub Desktop.
shell snippets and functions #shell #bash #sh

Inform with message

Use: message "Informing you about something"

message () {
    printf "\n";
    printf "\e[30;48;5;82m ${1} \e[0m "
    printf "\n";
}

Get current working directory from within script

WORKING_DIR="$(cd -P -- "$(dirname -- "$0")" && pwd -P)";

Split string

use: split_string "string,to split" , - the comma here is the separator. Also see man cut

split_string() {
	echo "$1" | cut -d"$2" -f1;
}

Change case of first letter to upper case

use: upper_case_word "sasha"

upper_case_word() {
    word=${1}
    therest=$(tr '[a-z]' '[A-Z]'<<<"${word:0:1}")
    echo "${therest}${word:1}"
}

Create bash script named OUTPUT_FILENAME on the fly

cat << 'MyHeredoc' >> ./OUTPUT_FILENAME
#!/bin/bash
var1="var1value"
var2="var2value"
MyHeredoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment