Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Setting my environmental PATH
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="nabl" data-source="post: 1052159" data-attributes="member: 43512"><p>$PATH is a variable in the Terminal that tell the computer which directories contain programs; that is, when you type a command such as <span style="font-family: 'Lucida Console'">ls</span> at the command-line, <span style="font-family: 'Lucida Console'">ls</span> is the name of a program in one of the locations specified by $PATH.</p><p></p><p>To figure out where a binary is, you can use the <span style="font-family: 'Lucida Console'">which</span> command. For example, typing <span style="font-family: 'Lucida Console'">which ls</span> should return something like <span style="font-family: 'Lucida Console'">/bin/ls</span>, which means that if you were to navigate to the /bin directory, you'd see the <span style="font-family: 'Lucida Console'">ls</span> program within.</p><p></p><p>To see the list of locations that are currently set for $PATH, you can type <span style="font-family: 'Lucida Console'">echo $PATH</span> at the command-line. It will return a list of directories (folders) separated by colons; these are the locations that will be checked in when you type a command name. In your case, you could copy those programs to a place like /usr/bin by typing the following command:</p><p>[code]sudo cp [I]program_name[/I] /usr/bin/[/code]</p><p>You'll need to type in your administrator password for the command to work because /usr/bin is a system directory (you get permission to move files there with the sudo command). This will copy the program you choose to that folder, at which point you can simply type its name and press Return to run it.</p><p></p><p>—</p><p></p><p>Alternatively, if you don't want to mess with using the <span style="font-family: 'Lucida Console'">sudo</span> command (which you should <strong>always</strong> be <em>extremely</em> careful with), you can add a new directory to your $PATH, from which you'll be able to run commands in the future. To do this, you need to edit either .bashrc or .bash_profile. For example, type [code]open -a TextEdit ~/.bash_profile[/code]</p><p>This will open your Bash profile (think of it as command-line preferences) in TextEdit, where you can add this line to the bottom:</p><p>[code]export PATH=$PATH:[I]/new/directory[/I][/code]</p><p>where <span style="font-family: 'Lucida Console'">/new/directory</span> is the folder you want to add to your $PATH. For example, if you make a folder in your Home directory called <span style="font-family: 'Lucida Console'">Programs</span>, you could run the command</p><p>[code]export PATH=$PATH:/Users/[I]your_username[/I]/Programs[/code]</p><p>and any binaries you put in that folder will run when you type their names at the command prompt.</p><p></p><p>Once you add that line to the file, save it and close it. For the change to take effect, type</p><p>[code]source ~/.bash_profile[/code]</p><p>This command tells the computer to look at .bash_profile and update any changes, which will in this case update your $PATH. To make sure it took effect, you can type <span style="font-family: 'Lucida Console'">echo $PATH</span> again, and you should see the same list you saw before with your new location at the end. Then, when you add any programs to the folder you chose (in this example, /Users/<em>your_username</em>/Programs), you'll simply be able to type their name at the command prompt and press Return to run them.</p><p></p><p>—</p><p></p><p>There's one final thing worth saying. If you only want to run a program once, you can navigate to the directory containing it and type the command name preceded by a period and a slash (<span style="font-family: 'Lucida Console'"><strong>./</strong></span>) to run the program regardless of whether the containing folder is in your $PATH or not. As an example, if your <span style="font-family: 'Lucida Console'">cuffs</span> program is in your Downloads folder, typing the following two commands would run it a single time:</p><p>[code]cd ~/Downloads</p><p>./cufflinks[/code]</p><p></p><p>—</p><p></p><p>Hopefully that little overview will help you out. <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" /> If you want to find out more, search for "bash path" on a search engine and you should get relevant results.</p></blockquote><p></p>
[QUOTE="nabl, post: 1052159, member: 43512"] $PATH is a variable in the Terminal that tell the computer which directories contain programs; that is, when you type a command such as [font="Lucida Console"]ls[/font] at the command-line, [font="Lucida Console"]ls[/font] is the name of a program in one of the locations specified by $PATH. To figure out where a binary is, you can use the [font="Lucida Console"]which[/font] command. For example, typing [font="Lucida Console"]which ls[/font] should return something like [font="Lucida Console"]/bin/ls[/font], which means that if you were to navigate to the /bin directory, you'd see the [font="Lucida Console"]ls[/font] program within. To see the list of locations that are currently set for $PATH, you can type [font="Lucida Console"]echo $PATH[/font] at the command-line. It will return a list of directories (folders) separated by colons; these are the locations that will be checked in when you type a command name. In your case, you could copy those programs to a place like /usr/bin by typing the following command: [code]sudo cp [I]program_name[/I] /usr/bin/[/code] You'll need to type in your administrator password for the command to work because /usr/bin is a system directory (you get permission to move files there with the sudo command). This will copy the program you choose to that folder, at which point you can simply type its name and press Return to run it. — Alternatively, if you don't want to mess with using the [font="Lucida Console"]sudo[/font] command (which you should [B]always[/B] be [I]extremely[/I] careful with), you can add a new directory to your $PATH, from which you'll be able to run commands in the future. To do this, you need to edit either .bashrc or .bash_profile. For example, type [code]open -a TextEdit ~/.bash_profile[/code] This will open your Bash profile (think of it as command-line preferences) in TextEdit, where you can add this line to the bottom: [code]export PATH=$PATH:[I]/new/directory[/I][/code] where [font="Lucida Console"]/new/directory[/font] is the folder you want to add to your $PATH. For example, if you make a folder in your Home directory called [font="Lucida Console"]Programs[/font], you could run the command [code]export PATH=$PATH:/Users/[I]your_username[/I]/Programs[/code] and any binaries you put in that folder will run when you type their names at the command prompt. Once you add that line to the file, save it and close it. For the change to take effect, type [code]source ~/.bash_profile[/code] This command tells the computer to look at .bash_profile and update any changes, which will in this case update your $PATH. To make sure it took effect, you can type [font="Lucida Console"]echo $PATH[/font] again, and you should see the same list you saw before with your new location at the end. Then, when you add any programs to the folder you chose (in this example, /Users/[i]your_username[/i]/Programs), you'll simply be able to type their name at the command prompt and press Return to run them. — There's one final thing worth saying. If you only want to run a program once, you can navigate to the directory containing it and type the command name preceded by a period and a slash ([font="Lucida Console"][b]./[/b][/font]) to run the program regardless of whether the containing folder is in your $PATH or not. As an example, if your [font="Lucida Console"]cuffs[/font] program is in your Downloads folder, typing the following two commands would run it a single time: [code]cd ~/Downloads ./cufflinks[/code] — Hopefully that little overview will help you out. :) If you want to find out more, search for "bash path" on a search engine and you should get relevant results. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Setting my environmental PATH
Top