{"id":54704,"date":"2023-01-18T06:11:04","date_gmt":"2023-01-18T14:11:04","guid":{"rendered":"https:\/\/linuxhint.com\/?p=54704"},"modified":"2023-01-21T13:01:43","modified_gmt":"2023-01-21T21:01:43","slug":"path_in_bash","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/path_in_bash\/","title":{"rendered":"Bash PATH Tutorial"},"content":{"rendered":"<div id=\"wpbody\">\n<p>The PATH is an environment variable that is used to store the location of the resources that contain executable files on the Linux operating system. The PATH is defined using slash(\/) and alphanumeric characters. Two types of PATH can be defined in Linux. One is an absolute path and the other is a relative path. The full location of a particular file or directory or file system is defined by an absolute path. The location that is relative to the current working directory is defined by the relative path. The $PATH variable mainly contains the location of those directories that execute frequently such as\/bin, \/usr\/bin, \/usr\/local\/bin, etc.<\/p>\n<h2>Different Examples of $PATH Variable<\/h2>\n<p>The different uses of the $PATH variable are shown in this part of the tutorial.<\/p>\n<p><strong>Example 1: Print the Current Value of the $PATH Variable<\/strong><\/p>\n<p>Create a Bash file with the following script that prints the current value of the $PATH variable using the \u201cecho\u201d and \u201cprintf\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<br \/>\n<span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;The current value of the PATH variable:<span class=\"es1\">\\n<\/span>&quot;<\/span> <br \/>\n<span class=\"co0\">#Print the values of the $PATH using 'printf' command<\/span><br \/>\n<span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;%s&quot;<\/span> <span class=\"re1\">$PATH<\/span><br \/>\n<span class=\"co0\">#Add two newlines<\/span><br \/>\n<span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\n<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;The current value of the PATH variable: &quot;<\/span><br \/>\n<span class=\"co0\">#Print the values of the $PATH using 'echo' command<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"re1\">$PATH<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\n<strong>Output:<\/strong><\/p>\n<p>The following output appears after executing the previous script:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/1-53.png\" alt=\"\" width=\"933\" height=\"519\" class=\"alignnone size-full wp-image-281427\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/1-53.png 933w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/1-53-300x167.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/1-53-768x427.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/1-53-810x451.png 810w\" sizes=\"(max-width: 933px) 100vw, 933px\" \/><br \/>\n<strong>Example 2: Set the $PATH Variable Temporarily<\/strong><\/p>\n<p>The new path location can be appended temporarily or permanently in the $PATH variable. The newly added path location works until the terminal closes. The method of adding a new location in the $PATH variable temporarily is shown in this tutorial. The new path can be appended with the content of the $PATH variable using the \u201cexport\u201d command in the terminal:<\/p>\n<p>Run the following command from the terminal to print the current content of the $PATH variable:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">echo<\/span> <span class=\"re1\">$PATH<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\nThe following output appears after executing the previous command:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/2-60.png\" alt=\"\" width=\"903\" height=\"256\" class=\"alignnone size-full wp-image-281428\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/2-60.png 903w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/2-60-300x85.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/2-60-768x218.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/2-60-810x230.png 810w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><br \/>\nCreate a Bash file named path2.bash with the following script to append a new path location in the $PATH variable and print the value of the $PATH variable after adding a new path location.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw3\">export<\/span> <span class=\"re2\">PATH<\/span>=<span class=\"re1\">$PATH<\/span>:<span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>fahmida<span class=\"sy0\">\/<\/span>Desktop<span class=\"sy0\">\/<\/span><span class=\"kw2\">bash<\/span><br \/>\n<span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;The current value of the PATH variable:<span class=\"es1\">\\n<\/span>&quot;<\/span> <br \/>\n<span class=\"co0\">#Print the values of the $PATH using 'printf' command<\/span><br \/>\n<span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;%s&quot;<\/span> <span class=\"re1\">$PATH<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\nRun the following command to execute the previous script:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">bash<\/span> path2.bash<\/div><\/div>\n<p>&nbsp;<br \/>\nThe following output appears after executing the previous script. The newly added path is shown in the output:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/3-57.png\" alt=\"\" width=\"900\" height=\"275\" class=\"alignnone size-full wp-image-281429\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/3-57.png 900w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/3-57-300x92.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/3-57-768x235.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/3-57-810x248.png 810w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><br \/>\nRe-open the terminal and check the $PATH variable again.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">echo<\/span> <span class=\"re1\">$PATH<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\nThe newly added path is not displayed in the following output because the path location was stored temporarily.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/4-56.png\" alt=\"\" width=\"906\" height=\"253\" class=\"alignnone size-full wp-image-281430\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/4-56.png 906w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/4-56-300x84.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/4-56-768x214.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/4-56-810x226.png 810w\" sizes=\"(max-width: 906px) 100vw, 906px\" \/><br \/>\n<strong>Example 3: Set the $PATH Variable Permanently Using ~\/.bashrc<\/strong><\/p>\n<p>The method of adding a new location in the $PATH variable temporarily is shown in this tutorial. The new path can be appended with the content of the $PATH variable permanently by adding the \u201cexport\u201d command of the previous example in the ~\/.bashrc file.<\/p>\n<p>Run the following command to open the ~\/.bashrc file with the root privilege:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> ~<span class=\"sy0\">\/<\/span>.bashrc<\/div><\/div>\n<p>&nbsp;<br \/>\nAdd the following line at the end of the file to append the new path location to the $PATH variable.<\/p>\n<p>Export PATH=$PATH:\/home\/fahmida\/Desktop\/bash.<\/p>\n<p>Run the following command after saving the ~\/.bashrc file to activate the changes done by the \u201cexport\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">source<\/span> ~<span class=\"sy0\">\/<\/span>.bashrc<\/div><\/div>\n<p>&nbsp;<br \/>\n<img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/5-56.png\" alt=\"\" width=\"901\" height=\"94\" class=\"alignnone size-full wp-image-281431\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/5-56.png 901w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/5-56-300x31.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/5-56-768x80.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/5-56-810x85.png 810w\" sizes=\"(max-width: 901px) 100vw, 901px\" \/><br \/>\nCreate a Bash file named path3.bash in the \u201c\/home\/fahmida\/Desktop\/bash\u201d location with the following script to print the value of the $PATH variable after adding a new path location:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;The current value of the PATH variable:<span class=\"es1\">\\n<\/span>&quot;<\/span> <br \/>\n<span class=\"co0\">#Print the values of the $PATH using 'printf' command<\/span><br \/>\n<span class=\"kw3\">printf<\/span> <span class=\"st0\">&quot;%s&quot;<\/span> <span class=\"re1\">$PATH<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\nRun the following command to execute the previous script from the \u201c\/home\/fahmida\/&#8221; location where the path3.bash file does not exist:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">bash<\/span> path3.bash<\/div><\/div>\n<p>&nbsp;<br \/>\nThe following output appears if the \u201c\/home\/fahmida\/Desktop\/bash\u201d location is appended properly in the $PATH variable. The newly added path is shown in the output:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/6-49.png\" alt=\"\" width=\"902\" height=\"276\" class=\"alignnone size-full wp-image-281432\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/6-49.png 902w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/6-49-300x92.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/6-49-768x235.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/6-49-810x248.png 810w\" sizes=\"(max-width: 902px) 100vw, 902px\" \/><br \/>\nRun the following command from the terminal to print the current content of the $PATH variable:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">echo<\/span> <span class=\"re1\">$PATH<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\nThe newly added path is shown in the output because the new path location was added permanently.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/7-40.png\" alt=\"\" width=\"910\" height=\"259\" class=\"alignnone size-full wp-image-281433\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/7-40.png 910w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/7-40-300x85.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/7-40-768x219.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/7-40-810x231.png 810w\" sizes=\"(max-width: 910px) 100vw, 910px\" \/><br \/>\n<strong>Example 4: Set the $PATH Variable Permanently Using \/etc\/profile<\/strong><\/p>\n<p>The $PATH variable can be set permanently by configuring the \/etc\/profile file. If the path of the Bash script file is stored permanently in the \/etc\/profile file, the script can be executed from any location. There is no write permission for the \/etc\/profile file by default. So, the write permission for the \/etc\/profile file is required to be set before adding the command of the new path location.<\/p>\n<p>Run the following command to add the write permission of \/etc\/profile file for all users:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">chmod<\/span> u+<span class=\"kw2\">w<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>profile<\/div><\/div>\n<p>&nbsp;<br \/>\nRun the following command to open the \/etc\/profile file with the root privilege:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>profile<\/div><\/div>\n<p>&nbsp;<br \/>\nAdd the following line to the end of the file:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw3\">export<\/span> <span class=\"re2\">PATH<\/span>=<span class=\"re1\">$PATH<\/span>:<span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>fahmida<span class=\"sy0\">\/<\/span>code<\/div><\/div>\n<p>&nbsp;<br \/>\nRun the following command after saving the \/etc\/profile file to activate the changes done by the \u201cexport\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">source<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>profile<\/div><\/div>\n<p>&nbsp;<br \/>\nCreate a Bash file named path4.bash in the \u201c\/home\/fahmida\/code\u201d location with the following script to print the value of the $PATH variable after adding a new path location.<\/p>\n<p>Run the following command to execute the previous script from the \u201c\/home\/fahmida\u201d location where the path4.bash file does not exist:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">bash<\/span> path4.bash<\/div><\/div>\n<p>&nbsp;<br \/>\nThe following output appears if the \u201c\/home\/fahmida\/code\u201d location is appended properly in the $PATH variable. The newly added path is shown in the output:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/8-43.png\" alt=\"\" width=\"913\" height=\"271\" class=\"alignnone size-full wp-image-281434\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/8-43.png 913w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/8-43-300x89.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/8-43-768x228.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2020\/02\/8-43-810x240.png 810w\" sizes=\"(max-width: 913px) 100vw, 913px\" \/><br \/>\nThe \/etc\/environment file can be used also to set the $PATH variable permanently.<\/p>\n<h2>Conclusion<\/h2>\n<p>The methods of executing the script from any location by storing the script location path in the $PATH variable are shown in this tutorial. The value of the $PATH variable can be modified permanently or temporarily. Three different ways of appending a value to the $PATH variable are shown here by multiple examples that will help the Bash users to know the use of the $PATH variable properly.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial on the methods of executing the script from any location by storing the script location path in the $PATH variable along with practical examples.<\/p>\n","protected":false},"author":33,"featured_media":282091,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1561],"tags":[],"class_list":["post-54704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-programming"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/54704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=54704"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/54704\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/282091"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=54704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=54704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=54704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}