-
-
Save thinkerbot/4404582 to your computer and use it in GitHub Desktop.
Iterate PATH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Not quite as fast as ifs, but pretty good! BASH specific. | |
path=":/a b/c:::x/y z:/a:/b:/c:/d:/e:/f:/g:/h:/i:/j" | |
IFS=: read -a arr <<<"$path" | |
printf "%s\n" "${arr[@]}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Probably as good as it gets. Fast, portable. Still doesn't handle empties. | |
# | |
# Extra fields | |
# path=":a::b" | |
# | |
# No extra field | |
# path="a:b:" | |
# | |
path=":/a b/c:::x/y z:/a:/b:/c:/d:/e:/f:/g:/h:/i:/j:" | |
print_path () { printf "%s\n" $@; } | |
IFS=: print_path "$path" | |
# Variation - slower | |
# (export IFS=:; printf "%s\n" $path) | |
# | |
# Variation - weirder | |
# IFS=: eval printf '%s\\n' \$path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Pretty fast, works. Needs filter for empty dir. | |
path=":/a b/c:::x/y z:/a:/b:/c:/d:/e:/f:/g:/h:/i:/j:" | |
while read -r dir; do echo "$dir"; done <<< "${path//:/$'\n'}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Faster, does not work for spaces. | |
path=":/a b/c:::x/y z:/a:/b:/c:/d:/e:/f:/g:/h:/i:/j:" | |
for dir in ${path//:/$'\n'} ; do echo "$dir" ; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment