- #
- ##
- %
- %%
#!/bin/bash TEST="i/am/a/very/big/dir" echo ${TEST#*/} echo ${TEST##*/} echo ${TEST%/*} echo ${TEST%%/*} # am/a/very/big/dir # dir # i/am/a/very/big # i echo ${TEST#*/*/} echo ${TEST%/*/*} # a/very/big/dir # i/am/a/very
I have omitted a leading slash in this string, which was a directory, so that it would print "i" instead of nothing. So you can use echo ${TEST%%/*} to get the first element and saving echo ${TEST#*/} on every iteration of a cycle that tokenizes the string into an array.
The last two expressions serve to show that if you're dealing with some hardcoded delimiters, you can further expand it so that it matches what you want precisely.