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
.
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With