Linux Tips and Tricks

Linux Tips and Tricks

Print/Read the $PATH variable in readable format

September17

The $PATH variable is specified as a list of one or more directory names separated by colon (:) characters

If you want to see the variable value, then just echo it.

my PATH variable contains the below directory names.

1
2
3
4
5
6
$ echo $PATH
/opt/sybase/ASE-15_0/jobscheduler/bin:/opt/sybase/ASE-15_0/bin:/opt/sybase/ASE-15_0/
install:/opt/sybase/ASEP/bin:/opt/sybase/DBISQL/bin:/opt/sybase/UAF-2_5/bin:/opt/sybase/
OCS-15_0/bin:/opt/sybase/ASE-15_0/jobscheduler/bin:/opt/sybase/ASE-15_0/bin:/opt/sybase/
ASE-15_0/install:/opt/sybase/ASEP/bin:/opt/sybase/DBISQL/bin:/opt/sybase/UAF-2_5/bin:
/opt/sybase/OCS-15_0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

well, the above output is not much readable format. Lets make/show it as a readable one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ echo $PATH | awk -v RS=: '1'
/opt/sybase/ASE-15_0/jobscheduler/bin
/opt/sybase/ASE-15_0/bin
/opt/sybase/ASE-15_0/install
/opt/sybase/ASEP/bin
/opt/sybase/DBISQL/bin
/opt/sybase/UAF-2_5/bin
/opt/sybase/OCS-15_0/bin
/opt/sybase/ASE-15_0/jobscheduler/bin
/opt/sybase/ASE-15_0/bin
/opt/sybase/ASE-15_0/install
/opt/sybase/ASEP/bin
/opt/sybase/DBISQL/bin
/opt/sybase/UAF-2_5/bin
/opt/sybase/OCS-15_0/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games

using some more commands, we can make it as readable.

Below is the some of the commands combination.

1
2
3
4
5
6
7
8
$ echo $PATH | tr ":" "\n"
$ echo $PATH | sed "s/:/\n/g"
$ echo $PATH | perl -pe 's/:/\n/g'
$ echo $PATH | awk '{gsub(":","\n")}1'
$ IFS=:; for i in $PATH; do echo $i; done
$ echo $PATH | perl -lane 's/:/\n/g;print $_'
In KSH
print - ${PATH//:/\\n}

In unix, we always have more ways to achieve the same goal. UNIX ROCKS 🙂

posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories