Skip to content

Instantly share code, notes, and snippets.

@blizzrdof77
Created May 11, 2018 22:10
Show Gist options
  • Save blizzrdof77/6867f6cbdff9acfb15875430ba724f20 to your computer and use it in GitHub Desktop.
Save blizzrdof77/6867f6cbdff9acfb15875430ba724f20 to your computer and use it in GitHub Desktop.
Split a string simple bash function
#!/bin/bash
# Split a string returning the specified index (or 2nd instance by default)
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Provide split delimiter and string"
else
delim="$1"
string="$2"
if [ -z "$3" ]; then
split_index=2
else
split_index="$3"
fi
echo "$2" | cut -d$1 -f $split_index
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment