Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split the contents of `$PATH` into distinct lines?

Tags:

Suppose echo $PATH yields /first/dir:/second/dir:/third/dir.

Question: How does one echo the contents of $PATH one directory at a time as in:

$ newcommand $PATH
/first/dir
/second/dir
/third/dir

Preferably, I'm trying to figure out how to do this with a for loop that issues one instance of echo per instance of a directory in $PATH.

like image 221
George Avatar asked Nov 02 '15 01:11

George


People also ask

How do you split paths?

The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute. You can use this cmdlet to get or submit only a selected part of a path.

How do you break a path in Python?

split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that.

What is the value of path split?

path. Split splits PATH immediately following the final slash, separating it into a directory and a base component. The returned values have the property that PATH = DIR + BASE . If there is no slash in PATH , it returns an empty directory and the base is set to PATH .


1 Answers

echo "$PATH" | tr ':' '\n'

Should do the trick. This will simply take the output of echo "$PATH" and replaces any colon with a newline delimiter.

Note that the quotation marks around $PATH prevents the collapsing of multiple successive spaces in the output of $PATH while still outputting the content of the variable.

like image 155
GammaOmega Avatar answered Oct 15 '22 19:10

GammaOmega