How to set environment variables in the Bash Shell

October 4, 2023 / How-to Guide

In Linux, the shell is a command-line interface that processes user input and executes commands.

Common shell types include Bourne Shell, Bash (Bourne Again Shell), C Shell, and Korn Shell, with Bash being the default for Red Hat Linux. It functions similarly to the Windows command line for users transitioning to Linux.

Follow steps:

  1. To check the shell you are currently using, enter the following command:
    $ echo $SHELL

    If you have been assigned the default shell by your web hosting provider, it will often be bash. Bash is located at “/bin/bash” and is automatically executed when a user successfully logs in. The default bash prompt, represented by $, can be customised according to user preferences. The appearance of the prompt may vary depending on the shell in use.

  2. Changing the Default Shell Prompt:
    The shell environment in Linux can be customised by setting variables, also known as environment variables. These variables help specify certain settings for processes running in the Linux environment.
    You can set an environment variable using the following syntax:

    VARIABLE=value

    For example, the command below displays the list of environment variables currently set on your system:

    $ env

    Linux sets many environment variables by default, and users can modify these as needed. Here are some commonly used environment variables:

    1. HOME: This variable sets the default home directory. For example, HOME=/home/stan means the home directory is /home/stan, assigned to the user stan. The cd command can be used to return to the home directory from anywhere.
    2. PATH: The PATH variable specifies directories the shell should search for executable programs. For example:
      PATH=/usr:/bin:/usr/local/bin:.

      Each directory in PATH is separated by a colon (:). Including a period (.) at the end that represents the current directory. When a command is entered, Linux searches these directories to locate the executable.

  3. Adding a New Directory to PATH:
    To add a new directory without replacing the current PATH value, use:

    PATH=$PATH:/newdirectory

    This appends /newdirectory to the existing PATH. Always use $VARIABLE to refer to the current value of a variable.

    1. PS1: This variable controls how your shell prompt looks. The default bash prompt is $, but it can be changed to something like PS1=boss, which would display as:
      boss> ls
    2. SHELL: This variable defines the path of the shell program, such as /bin/bash or /bin/ksh for the Korn shell. You can switch shells by changing this variable.
  4. Making Changes Permanent:
    To make any of these environment variable changes permanent, add them to your .profile file located in your home directory. Each command should be placed on a new line within the file.
    Note: Ensure that executable files have the appropriate permissions (e.g., [755]).

That is it! Hope you liked our article. If you encounter any issues, feel free to contact our support staff.

Spread the love