Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over $PATH variable using shell script

If I type echo $PATH in the terminal I get the following result:

/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/fnorbert/.local/bin:/home/fnorbert/bin

I want to iterate over these paths using a shell script, but I do not know how to do that.
I tried the following:

for i in 1 2 3  
do
    echo $PATH | cut -d':' -f$i
done

This prints the first three paths, but I want to represent every path with the variable i if it is possible.

like image 866
Fogarasi Norbert Avatar asked Jun 13 '17 14:06

Fogarasi Norbert


Video Answer


1 Answers

You can use read with delimiter set as :

while read -d ':' p; do
   echo "$p"
done <<< "$PATH:"
like image 115
anubhava Avatar answered Nov 23 '22 23:11

anubhava