The PATH environment variable is a crucial part of the Linux operating system, as it contains a list of directories where the system looks for executable files. When you run a command in the terminal, Linux searches for the corresponding executable in the directories listed in the PATH variable. Occasionally, you may find the need to split the PATH variable into separate lines, making it easier to read, modify, or manage. This tutorial provides 3 methods how to split PATH environment variable into lines on Linux.
Method 1 - tr command
The tr
is a command tool for character translation. We can use it to replace the colon (:
) separator in the PATH variable with newline characters to split it into lines.
echo $PATH | tr ':' '\n'
This command will display the PATH variable with each directory on a separate line, making it more accessible to view.
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
...
Method 2 - awk command
The awk
command is a versatile text processing tool on Linux. We can use it to split the PATH variable and print each directory on a new line.
echo $PATH | awk '1' RS=':'
The pattern '1'
is a common shorthand in awk
that evaluates to true, meaning it will print each line by default. In this case, since there is no specific action specified, awk
performs the default action, which is to print the lines.
The RS
(Record Separator) variable is set to a colon (:
). By doing this, awk
treats each directory in the PATH variable as a separate record or line, effectively splitting the PATH variable into lines based on colons.
Method 3 - sed command
The sed
is another powerful tool for text manipulation on Linux. We can utilize it to substitute the colons (:
) in the PATH variable with newline characters.
echo $PATH | sed 's/:/\n/g'
Leave a Comment
Cancel reply