Skip to main content
deleted 387 characters in body
Source Link

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Or, a version which uses no external programs:

path_no_dup () { local o=() r= declare -A s=() IFS=: read -a e <<< "$1" for i in "${e[@]}"; do [[ ! ${s[$i]} ]] && { s[$i]=1;o+=("$i"); } done printf -v r '%s:' "${o[@]}" # Do whatever you like with "${r%:}" echo "${r%:}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$ 

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Or, a version which uses no external programs:

path_no_dup () { local o=() r= declare -A s=() IFS=: read -a e <<< "$1" for i in "${e[@]}"; do [[ ! ${s[$i]} ]] && { s[$i]=1;o+=("$i"); } done printf -v r '%s:' "${o[@]}" # Do whatever you like with "${r%:}" echo "${r%:}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$ 

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$ 
Added another version
Source Link

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Or, a version which uses no external programs:

path_no_dup () { local o=() r= declare -A s=() IFS=: read -a e <<< "$1" for i in "${e[@]}"; do [[ ! ${s[$i]} ]] && { s[$i]=1;o+=("$i"); } done printf -v r '%s:' "${o[@]}" # Do whatever you like with "${r%:}" echo "${r%:}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$ 

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$ 

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Or, a version which uses no external programs:

path_no_dup () { local o=() r= declare -A s=() IFS=: read -a e <<< "$1" for i in "${e[@]}"; do [[ ! ${s[$i]} ]] && { s[$i]=1;o+=("$i"); } done printf -v r '%s:' "${o[@]}" # Do whatever you like with "${r%:}" echo "${r%:}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$ 
Source Link

This is my version:

path_no_dup () { local IFS=: p=(); while read -r; do p+=("$REPLY"); done < <(sort -u <(read -ra arr <<< "$1" && printf '%s\n' "${arr[@]}")); # Do whatever you like with "${p[*]}" echo "${p[*]}" } 

Usage: path_no_dup "$PATH"

Sample output:

rany$ v='a:a:a:b:b:b:c:c:c:a:a:a:b:c:a'; path_no_dup "$v" a:b:c rany$