0

I was recently trying to created all unique path names, managed to get the unique names echo $PATH | tr ":" "\n" | sort -u however I didn't really feel like appending the export PATH = $PATH: to them manually or through a for loop.

is there any way I can for that, something in the lines for

'echo $PATH | tr ":" "\n" | sort -u | append each 'export PATH=\$PATH:'

Mat
  • 202,337
  • 40
  • 393
  • 406
rishi
  • 155
  • 1
  • 6
  • Your title implies that you want to append something to each element of `$PATH` but the question suggests that you want to modify `$PATH` so that it only contains unique entries. – Tom Fenech Nov 26 '13 at 18:03
  • Well that was the just one of the usecase. I've found myself doing this a lot of time. – rishi Nov 26 '13 at 18:44

1 Answers1

2

How about this:

path=$(echo $PATH | tr ":" " ")
append="something"
for p in $path; do s=$s$p$append":"; done

$s now contains your new path, with "something" appended to each element.

edit

you could use printf (thanks to this answer):

append="something"
path=($(echo $PATH | tr ":" " ")) # notice the additional parentheses
printf "%s$append:" "${path[@]}"

not really shorter but maybe a bit fancier!

Community
  • 1
  • 1
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141