Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active January 2, 2016 17:39
Show Gist options
  • Select an option

  • Save coderofsalvation/8338454 to your computer and use it in GitHub Desktop.

Select an option

Save coderofsalvation/8338454 to your computer and use it in GitHub Desktop.
returns part of string
# returns part of string
# @param string input
# @param string startoffset
# @param string number of characters
#
# usage: substr foobar 0 3 (outputs 'foo')
# echo "foobar" | substr 0 3 (outputs 'foo')
# short="$( substr "foobar" 0 3 )"; echo "$short" (outputs 'foo')
function substr(){
if [[ -n "$3" ]]; then echo "${1:$2:$3}";
else cat - | while read line; do a="$line"; b="$1"; c="$2"; echo "${a:b:c}"; done; fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment