Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display $PATH as a list

Tags:

linux

bash

how can I display all the paths from $PATH like this?

Path 1: usr/bin
Path 2: /bin
... and so on.

I'm very new to this so I have no idea what to do with grep and how to display it like this. Thx in advance

like image 942
jale jole Avatar asked Dec 18 '22 16:12

jale jole


2 Answers

I'm not quite sure why you want this? tr can translate character:

$ echo "$PATH" | tr ':' $'\n'
/usr/bin
/bin
...

tr will translate SET1 (:) to SET2 (newlines)

Alternative you can use AWK to format it a little nicer:

$ awk '{ print "Path: "NR, $0 }' RS=: <<< "$PATH"
Path 1: /usr/bin
Path 2: /bin
...
like image 67
Andreas Louv Avatar answered Dec 22 '22 00:12

Andreas Louv


In a shell script you could do this:

#!/bin/bash

IFS=':' read -ra ITEMS <<< "$PATH"
for i in "${ITEMS[@]}"; do
    echo "$i"
done

This will print out each individual PATH setting in the list on a separate line.

If you want to do something else with it you just change the command(s) inside the for loop.

like image 32
kathmann Avatar answered Dec 21 '22 22:12

kathmann