Skip to main content
added 8 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

Recent bash versions (>= 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; set -f; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array
  • ${a[$i]+_} is a parameter expansion meaning: _ is substituted if and only if a[$i] is set. This is similar to ${parameter:+word} which also tests for not-null. Thus, in the following evaluation of the conditional, the expression _ (i.e. a single character string) evaluates to true (this is equivalent to -n _) - while an empty expression evaluates to false.

Recent bash versions (>= 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array
  • ${a[$i]+_} is a parameter expansion meaning: _ is substituted if and only if a[$i] is set. This is similar to ${parameter:+word} which also tests for not-null. Thus, in the following evaluation of the conditional, the expression _ (i.e. a single character string) evaluates to true (this is equivalent to -n _) - while an empty expression evaluates to false.

Recent bash versions (>= 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; set -f; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array
  • ${a[$i]+_} is a parameter expansion meaning: _ is substituted if and only if a[$i] is set. This is similar to ${parameter:+word} which also tests for not-null. Thus, in the following evaluation of the conditional, the expression _ (i.e. a single character string) evaluates to true (this is equivalent to -n _) - while an empty expression evaluates to false.
add explaination of the ${parameter+word} parameter expansion
Source Link
maxschlepzig
  • 59.6k
  • 53
  • 224
  • 297

Recent bash versions (>>= 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array
  • ${a[$i]+_} is a parameter expansion meaning: _ is substituted if and only if a[$i] is set. This is similar to ${parameter:+word} which also tests for not-null. Thus, in the following evaluation of the conditional, the expression _ (i.e. a single character string) evaluates to true (this is equivalent to -n _) - while an empty expression evaluates to false.

Recent bash versions (> 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array

Recent bash versions (>= 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array
  • ${a[$i]+_} is a parameter expansion meaning: _ is substituted if and only if a[$i] is set. This is similar to ${parameter:+word} which also tests for not-null. Thus, in the following evaluation of the conditional, the expression _ (i.e. a single character string) evaluates to true (this is equivalent to -n _) - while an empty expression evaluates to false.
Source Link
maxschlepzig
  • 59.6k
  • 53
  • 224
  • 297

Recent bash versions (> 4) also of associative arrays, i.e. you can also use a bash 'one liner' for it:

PATH=$(IFS=:; declare -A a; NR=0; for i in $PATH; do NR=$((NR+1)); \
       if [ \! ${a[$i]+_} ]; then if [ $NR -gt 1 ]; then echo -n ':'; fi; \
                                  echo -n $i; a[$i]=1; fi; done)

where:

  • IFS changes the input field separator to :
  • declare -A declares an associative array