How to Display the $PATH Variable on Newlines in Linux

The PATH environment variable specifies a set of directories where your commands go, and if you type a command with nothing else in front of it the Linux shell looks for it in one of these directories. You can always type echo $PATH at the command line to see a full list of these directories, but they’re awkwardly separated by colons like they were all attached. A few different commands can display these directories each on newlines, and you can run these either from a shell or a script. This makes it far easier to read if you’re trying to pick out a single directory that you have some commands stored in.

You’ll naturally need a command line for this, so start a terminal from the Dash or perhaps search for it on the Dash. You could also hold down Ctrl, Alt and T to start one up or look for LXTerminal on the System Tools menu in LXDE. There’s no need to use sudo or have administrator access for this one. Each command may be run as a regular user, and in fact the $PATH environment variable is tied to a specific user account. What makes this process even easier is the fact that you’ll only ever need to run one single command to accomplish it.

Method 1: Using a Shell Builtin Command

At the command prompt, type echo “${PATH//:/$’\n’}” and then push enter to receive a full list of each individual directory in your path on a separate line. This uses the parameter expansion programming technique with a shell builtin, so it should work with pretty much any version of the bash shell around. It might even work in some other Unix-based operating systems besides Linux like FreeBSD, though your mileage may vary.

Since this command is somewhat awkward to write, you might want to copy it and paste it over in your shell or script. If you’re pasting it into a terminal window, then remember to either click on the Edit menu and then select Paste or hold down Shift, Ctrl and V at the same time since plain Ctrl+V won’t work in a terminal window.

Method 2: Using sed or tr with $PATH

Type sed ‘s/:/\n/g’ <<< “$PATH” and then push enter to use the stream editor, which will have precisely the same result as the above command. Once again, if you’d prefer, you could copy it and then use either click the Edit menu and select Paste or hold down Shift+Ctrl+V to paste it in a terminal window. Whether to use this or the builtin command is merely a matter of preference as they achieve the same exact result.

You could also use the tr program to once more achieve the exact same result, which may be useful if you don’t have sed for some reason. Issue tr ‘:’ ‘\n’ <<< “$PATH” at the prompt and push enter. You could also copy and paste it the same way. The end result is completely identical in spite of whichever way you elect to do it.

None of these methods are at all incorrect. Regardless of which method you use, keep in mind that it tends to be a matter of what you happen to issue at the time. In all of these cases you’ll only need to issue a single command to get it work and there shouldn’t be any playing around since you’ll merely need to type it and then move on. Likewise, each could be added to a script without any modification.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.