Little Girl's Mostly Linux Blog

ExportPaths

Export Paths

This page was last updated on October 25, 2009.

Introduction

There may come a time when someone will give you instructions to do something on the command line, and part of those instructions will involve exporting a path. This may sound somewhat ominous at first (it did to me), but it turns out that exporting paths can be quite useful.

When you export a path, you’re making it available to the operating system. You can export a path temporarily to make it available for the current terminal session, or you can export it permanently.

As an example, let’s say that you like to write Bash scripts, you keep them in the /home/username/scripts directory, and you run them from time to time. To run any of the scripts, you would open a terminal window, change to the /home/username/scripts directory, and type the command to run the script, or you would open a terminal window and type the command to run the script, including the full path to the /home/username/scripts directory as part of the command.

To make life easier, you could export the path to the /home/username/scripts directory so the sytem can use it automatically. From then on, to run any of the scripts, you would open a terminal window and type the command to run the script. Notice that you no longer need to change to the directory the scripts are in or add the full path to the command.

 

Display the paths I’m currently exporting

  • Type this command to display the paths that are currently being exported:
  • echo $PATH
    • Example output:
    • /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

      This means that files in these directories are in your path:

      • /usr/local/sbin
      • /usr/local/bin
      • /usr/sbin
      • /usr/bin
      • /sbin
      • /bin
      • /usr/games

Export path temporarily

This will temporarily export a path until you close the current terminal session. Whatever path you export will be added to the exported paths your system currently has (see above for displaying the paths you currently have exported) rather than replacing them.
  • Open a terminal window and type this command, replacing pathtodirectory with the path to the directory you’d like to export:
  • export PATH="$PATH:/pathtodirectory"
    • Example:
    • export PATH="$PATH:/home/littlegirl/Documents"
  • To export more than one path, add additional paths with each separated from the one before it by a colon, as in this example:
  • export PATH="$PATH:/pathtodirectory1:/pathtodirectory2:/pathtodirectory3"
    • Example:
    • export PATH="$PATH:/home/littlegirl/scripts:/home/littlegirl/bin:/home/littlegirl/myprograms"

Export path permanently

This will make an export permanent so that you don’t have to export it temporarily every time you open a terminal session.
  1. Open your /home/username/.bashrc file in a text editor:

    • From the GUI:

      1. Enable Show hidden files in your file manager.
      2. Open your /home/username directory.
      3. Left-click or double left-click (depending on your settings) the file, or right-click it and select the Open with option and choose a text editor.
    • From the command line:

      • Ubuntu users type this command, replacing username with your user name:
      • gedit /home/username/.bashrc
      • Kubuntu users type this command, replacing username with your user name:
      • kate /home/username/.bashrc
  2. Look for this line in the file:
  3. export PATH
  4. If it’s there, insert this line above it, replacing pathtodirectory with the path to the directory you’d like to export:
  5. PATH="$PATH:/pathtodirectory"
  6. If it’s not there, insert these two lines at the bottom of your /home/username/.bashrc file, replacing pathtodirectory with the path to the directory you’d like to export:
  7. PATH="$PATH:/pathtodirectory"
    export PATH
    • Example:
    • PATH="$PATH:/home/littlegirl/scripts"
      export PATH
  8. To add more paths, insert more PATH lines above the export PATH line:
  9. PATH="$PATH:/pathtodirectory1"
    PATH="$PATH:/pathtodirectory2"
    PATH="$PATH:/pathtodirectory3"
    export PATH
  10. Save the file.
  11. Close the file.
  12. Open a terminal window and type this command to verify that you exported the paths properly:
  13. echo $PATH

    You should see the directories you exported at the end of the path line.


Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: