Thursday, September 27, 2012

Globally modifying the PATH variable

I have several scripts I've wrote through time to automatize my daily tasks (recreate the Arch mirror list, rename downloaded PDFs to comply with a personal format, change the current keyboard layout, etc.).

For some time I executed the scripts using its path, as I used them no more than one time per day. Now that I am using some of those scripts very frequently, I decided to add them permanently to my PATH environment variable.

To accomplish this, I used Bash's (my initial shell) /etc/profile mechanism to globally modify the PATH variable. In Arch Linux (and some other Linux flavors), the default /etc/profile script calls all the scripts contained in /ets/profile.d/, so I just added a script there that does what I need.

#!/bin/bash
#
# adding personal scripts directory to path
#
PATH=$PATH:/home/my-user-name/scripts
export PATH

Finally, to keep this script in my home directory, I added a soft link to profile.d directory, instead of actually place the script there.

ln -s /etc/profile.d/personal_scripts.sh /home/my-user-name/personal_scripts.sh

2 comments:

  1. This approach needs some admin privileges, if you are in a restricted enviroment, you can simple put in your bashrc, maybe an alias wirh absolute path may help: "alias my-recurrent-script='/path/to/my/script with-common-parameters'".

    If you want share your awesome script with other users, you can drop it on /usr/local/bin, a pro tip for that is share without sh extension, that is "awesome-script.sh" turns on just "awesome-script".

    Glad to read your experencies on Arch Linux.

    ReplyDelete
    Replies
    1. I completely agree. I think that it is actually better to use this method. as you do not need to set it up all over again if you need to reinstall linux for a particular reason (assuming you have /home mounted in its own partition).

      Delete