Create a personal bin directory (~/bin) and run scripts without specifying their full path
Sometimes there is a need to download some command-line application (like ExifTool) and run it from the Terminal, but without putting it into a main system directory for the bin files - /usr/local/bin/
.
Since I’m not going to leave a bin file there for a long time, but instead I’m going to use it a couple of times and then delete it, then I would like to have a personal directory for temporary bin files instead of littering the main directory. This directory is also useful for putting your own script files there.
In this tutorial, you’ll learn how to create a personal bin directory at your home directory and run a command-line applications or your own scripts from this directory without specifying their full path.
The following instruction is intended for macOS users.
If you use Linux, you may need to change some lines.
It will not work if you use Windows.
Launch the Terminal app from the Utilities folder of your Applications folder, or use Spotlight to find it.
In the Terminal window, type the following to browse to your home directory:
cd ~/
Check out if the .bash_profile
file exist in our user’s home directory.
ls -la
... drwx------ 2 arthur staff 64 Feb 28 15:17 .Trash -rw------- 1 arthur staff 11793 Feb 12 20:24 .bash_history drwxr-xr-x 3 arthur staff 96 Jan 18 10:01 .bash_imb -rw-------@ 1 arthur staff 517 Feb 28 10:04 .bash_profile drwx------ 643 arthur staff 20576 Feb 28 10:03 .bash_sessions drwxr-xr-x 3 arthur staff 96 Oct 7 18:30 .bundle drwx------ 5 arthur staff 160 Jul 10 2017 .cache drwx------ 6 arthur staff 192 Jul 10 2017 .config ...
If the .bash_profile
file doesn’t exist, we will create it with the following command:
touch .bash_profile
Note! The name begins with a dot (
.
).
Open up our .bash_profile
file in the TextEdit application:
open -a TextEdit .bash_profile
Add the following line to the .bash_profile
file:
PATH=~/bin:$PATH
PATH
is marked for export by default, so we don’t need to useexport
like so:export PATH=~/bin:$PATH
.
~/
is a shorthand for the user’s home directory, so we don’t need to set full path like so:export PATH=/Users/arthur/bin:$PATH
.
To apply the changes immediately to our bash profile without having to log out, we can run the following command:
source .bash_profile
Done. Now we can run a command-line applications or our own scripts from our personal bin directory that located in our user’s directory.
Testing
Let’s now try to test our new bin directory. For example, I will use the php-cs-fixer
utility.
Download the latest version of the php-cs-fixer
and rename file to php-cs-fixer
:
For this we can use wget
or curl
:
wget http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer
curl -L http://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -o php-cs-fixer
Give the right to execution:
sudo chmod a+x php-cs-fixer
This command will prompt us to enter our computer password.
Move file to our personal bin directory:
mv php-cs-fixer ~/bin/
Now we can confirm that we have the php-cs-fixer
tool available for use by using the which
:
which php-cs-fixer
/Users/Arthur/bin/php-cs-fixer
That’s it, we’re done. So simple isn’t it?
If this article has helped you then please leave a comment
Thanks for reading!
Arthur is a designer and full stack software engineer. He is the founder of Space X-Chimp and the blog My Cyber Universe. His personal website can be found at arthurgareginyan.com.