Setting Environment Variables

Problem

You need to modify your operating environment, for example, to change your shell’s PATH setting.

Solution

Edit the appropriate shell startup file. Under Windows NT-based systems, another alternative is to use the System control panel.

Discussion

The shell or command interpreter you use to run programs from the command-line prompt includes an environment in which you can store variable values. Some of these variables are used by the shell itself. For example, it uses PATH to determine which directories to look in for programs such as mysql. Other variables are used by other programs (such as PERL5LIB, which tells Perl where to look for library files used by Perl scripts).

Your shell determines the syntax used to set environment variables, as well as the startup file in which to place the settings. Typical startup files for various shells are shown in the following table. If you’ve never looked through your shell’s startup files, it’s a good idea to do so to familiarize yourself with their contents.

Shell

Possible startup files

csh, tcsh

.login, .cshrc, .tcshrc

sh, bash, ksh

.profile .bash_profile, .bash_login, .bashrc

DOS prompt

C:\AUTOEXEC.BAT

The following examples show how to set the PATH variable so that it includes the directory where the mysql program is installed. The examples assume there is an existing PATH setting in one of your startup files. If you have no PATH setting currently, simply add the appropriate line or lines to one of the files.

Tip

If you’re reading this section because you’ve been referred here from another chapter, you’ll probably be more interested in changing some variable other than PATH. The instructions are similar because you use the same syntax.

The PATH variable lists the pathnames for one or more directories. If an environment variable’s value consists of multiple pathnames, it’s conventional under Unix to separate them using the colon character (:). Under Windows, pathnames may contain colons, so the separator is the semicolon character ( ;).

To set the value of PATH, use the instructions that pertain to your shell:

  • For csh or tcsh, look for a setenv PATH command in your startup files, then add the appropriate directory to the line. Suppose your search path is set by a line like this in your .login file:

    setenv PATH /bin:/usr/bin:/usr/local/bin

    If mysql is installed in /usr/local/mysql/bin, add that directory to the search path by changing the setenv line to look like this:

    setenv PATH /usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin

    It’s also possible that your path will be set with set path, which uses different syntax:

    set path = (/usr/local/mysql/bin /bin /usr/bin /usr/local/bin)
  • For a shell in the Bourne shell family such as sh, bash, or ksh, look in your startup files for a line that sets up and exports the PATH variable:

    export PATH=/bin:/usr/bin:/usr/local/bin

    The assignment and the export might be on separate lines:

    PATH=/bin:/usr/bin:/usr/local/bin
    export PATH

    Change the setting to this:

    export PATH=/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin

    Or:

    PATH=/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin
    export PATH
  • Under Windows, check for a line that sets the PATH variable in your AUTOEXEC.BAT file. It might look like this:

    PATH=C:\WINDOWS;C:\WINDOWS\COMMAND

    Or like this:

    SET PATH=C:\WINDOWS;C:\WINDOWS\COMMAND

    Change the PATH value to include the directory where mysql is installed. If this is C:\mysql\bin, the resulting PATH setting looks like this:

    PATH=C:\mysql\bin;C:\WINDOWS;C:\WINDOWS\COMMAND

    Or:

    SET PATH=C:\mysql\bin;C:\WINDOWS;C:\WINDOWS\COMMAND
  • Under Windows NT-based systems, another way to change the PATH value is to use the System control panel (use its Environment or Advanced tab, whichever is present). In other versions of Windows, you can use the Registry Editor application. Unfortunately, the name of the Registry Editor key that contains the path value seems to vary among versions of Windows. For example, on the Windows machines that I use, the key has one name under Windows Me and a different name under Windows 98; under Windows 95, I couldn’t find the key at all. It’s probably simpler just to edit AUTOEXEC.BAT.

After setting an environment variable, you’ll need to cause the modification to take effect. Under Unix, you can log out and log in again. Under Windows, if you set PATH using the System control panel, you can simply open a new DOS window. If you edited AUTOEXEC.BAT instead, restart the machine.

Get MySQL Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.