Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print $PATH line by line

Tags:

shell

path

sed

The usual code to check the $PATH variable echo $PATH often print out some long and cumbersome line like: /anaconda2/bin:/anaconda2/condabin:/usr/local/Cellar/root/6.16.00/bin:~/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
which has bad readability. Is there any way I can print all the paths line by line instead on a single line? Like this:

/anaconda2/bin
/anaconda2/condabin
/usr/local/Cellar/root/6.16.00/bin
~/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin
/opt/X11/bin
like image 214
Cheng Tao Avatar asked Mar 05 '19 14:03

Cheng Tao


3 Answers

Using tr:

echo $PATH | tr : '\n'
like image 97
Arkadiusz Drabczyk Avatar answered Nov 08 '22 07:11

Arkadiusz Drabczyk


Try this code:

echo $PATH | sed 's|:|\
|g'

sed is a stream editor, and what it does here is searching for every instance of character ':' and replace it with a \newline character. 'g' is a flag indicating that the find & replace is global (the default is to replace one instance per line).

You can also wrap the code around a function in bash profile:

function printpath() {
  echo $PATH | sed 's|:|\
|g'
}

so you can just type printpath to get the desired output.

like image 3
Cheng Tao Avatar answered Nov 08 '22 07:11

Cheng Tao


echo -e ${PATH//:/\\n}

use variable substitution to replace the colon with a newline

${var//Pattern/Replacement}

-e enable interpretation of backslash escapes

If -e is in effect, the following sequences are recognized:

\\ backslash

like image 2
Chris Avatar answered Nov 08 '22 07:11

Chris