Parsing and Printing $PATH Using Unix Parsing and Printing $PATH Using Unix unix unix

Parsing and Printing $PATH Using Unix


Yet another way:

echo $PATH | tr : '\n'

or:

tr : '\n' <Path.txt


The tr solution is the right one but if you were going to use awk then there'd be no need for a loop:

$ echo "$PATH"/usr/local/bin:/usr/bin:/cygdrive/c/winnt/system32:/cygdrive/c/winnt$ echo "$PATH" | awk -F: -v OFS="\n" '$1=$1'/usr/local/bin/usr/bin/cygdrive/c/winnt/system32/cygdrive/c/winnt


I have a Perl script that I use for this:

#!/usr/bin/env perl##   "@(#)$Id: echopath.pl,v 1.8 2011/08/22 22:15:53 jleffler Exp $"##   Print the components of a PATH variable one per line.#   If there are no colons in the arguments, assume that they are#   the names of environment variables.use strict;use warnings;@ARGV = $ENV{PATH} unless @ARGV;foreach my $arg (@ARGV){    my $var = $arg;    $var = $ENV{$arg} if $arg =~ /^[A-Za-z_][A-Za-z_0-9]*$/;    $var = $arg unless $var;    my @lst = split /:/, $var;    foreach my $val (@lst)    {        print "$val\n";    }}

I invoke it like:

echopath $PATHechopath PATHechopath LD_LIBRARY_PATHechopath CDPATHechopath MANPATHechopath $CLASSPATH

etc. You can specify the variable name, or the value of the variable; it works both ways.