Skip to main content
Engineering LibreTexts

13-A.1: How to Customize the Bash Shell

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    1.6 Given a scenario, configure localization options.
    4.4 Given a scenario, analyze and troubleshoot application and hardware issues.
    5.1 Given a scenario, deploy and execute basic Bash scripts.

    Objectives of this Module

    In this lesson, you will:

    • Learn how to customize the Bash shell environment for script execution.
    • Determine concepts fundamental to both scripting and programming.
    • Create a simple Bash script and successfully execute it.
    • Create more complicated Bash scripts that include flow control like conditional statements and loops.

    Linux Shell

    The most generic sense of the term "Linux shell" means any program that users employ to type commands. A shell hides the details of the underlying operating system and manages the technical details of the operating system kernel interface, which is the lowest-level, or "inner-most" component of most operating systems.

    In the Linux operating systems, users typically have many choices of command-line interpreters for interactive sessions. When a user logs into the system interactively, a shell program is automatically executed for the duration of the session. The type of shell, which may be customized for each user, is typically stored in the user's profile, for example in the local passwd file or in a distributed configuration system such as NIS or LDAP; however, the user may execute any other available shell interactively.

    The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

    In Linux, processes spawn other processes, and each process has its own process identification number - the process ID. In the following example we initially see the bash process with a process ID (PID) of 14274. Notice that the other process has a parent PID (PPID) that is the PID of the bash process. So, the bash process spawned the ps -f process in this parent-child analogy.

    Then a new bash process is started from the command line, and we see that its PPID is the original process ID. Now the ps -f command is run again, BUT, the PPID is the PID of the new bash process.

    pbmac@pbmac-server $ ps -f
    UID        PID  PPID  C STIME TTY          TIME CMD
    pbmac      591 14274  0 14:59 pts/0    00:00:00 ps -f
    pbmac    14274 14264  0 Nov16 pts/0    00:00:00 bash
    pbmac@pbmac-server $ bash
    pbmac@pbmac-server $ ps -f
    UID        PID  PPID  C STIME TTY          TIME CMD
    pbmac      599 14274  0 14:59 pts/0    00:00:00 bash
    pbmac      673   599  0 14:59 pts/0    00:00:00 ps -f
    pbmac    14274 14264  0 Nov16 pts/0    00:00:00 bash

    Linux Shells Scripts

    A shell script is a computer program designed to be run by the Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.

    There is a great deal that creates the shell environment in Linux, and it is important to understand all of these pieces to properly use the shell to its fullest extent.

    Shell Variables

    What are Variables For?

    Whether you need variables in Bash depends on what you do in a terminal. For some users, variables are an essential means of managing data, while for others they’re minor and temporary conveniences, and for still others, they may as well not exist.

    Ultimately, variables are a tool. You can use them when you find a use for them, or leave them alone in the comfort of knowing they’re managed by your OS. Knowledge is power, though, and understanding how variables work in Bash can lead you to all kinds of unexpected creative problem-solving.

    How to Set a Variable

    You don’t need special permissions to create a variable. They’re free to create, free to use, and generally harmless. In a Bash shell (on Linux and Mac), you can set them by defining a variable name, and then setting its value. 

    Setting variables can be a common thing for people who use the shell often, so the process has become somewhat informal. When a string is followed by an equal sign (=) and a value, Bash quietly assumes that you’re setting a variable. The following example creates a new variable called FOO and sets the value to the string /home/pbmac/Documents:

     pbmac@pbmac-server $ FOO="/home/pbmac/Documents

    Success is eerily silent, so you may not feel confident that your variable got set. You can see the results for yourself with the echo command, recalling your variable by prepending it with a dollar sign ($). To ensure that the variable is read exactly as you defined it, you can also wrap it in braces and quotes. Doing this preserves any special characters that might appear in the variable; in this example, that doesn’t apply, but it’s still a good habit to form:

    pbmac@pbmac-server $ echo "${FOO}"

    Setting variables can be a common thing for people who use the shell often, so the process has become somewhat informal. When a string is followed by an equal sign (=) and a value, Bash quietly assumes that you’re setting a variable, making the declare keyword unnecessary:

     pbmac@pbmac-server $ FOO="/home/pbmac/Documents" 

    Variables usually are meant to convey information from one system to another. In this simple example, your variable is not very useful, but it can still communicate information. For instance, because the content of the FOO variable is a file path, you can use the variable as a shortcut to the ~/Documents directory:

    pbmac@pbmac-server $ pwd
    pbmac@pbmac-server $cd $FOO
    pbmac@pbmac-server $ pwd

    Variables can be any non-reserved string (along with integers and underscores). They don't have to be capitalized, but they often are so that they're easy to identify as variables.

    How to Clear a Variable

    You can clear a variable with the unset command:

    pbmac@pbmac-server $ unset FOO
    pbmac@pbmac-server $ echo $FOO

    Environment Variables

    Environment variables contain information about your login session, stored for the system shell to use when executing commands. They exist whether you’re using Linux, Mac, or Windows. Many of these variables are set by default during installation or user creation.

    Environment variables are no different, technically, than variables. They can be set, recalled, and cleared with exactly the same syntax used for variables.

    You don’t often use environment variables directly. They’re referenced by individual applications and daemons as needed. For instance, your home directory is set as an environment variable when you log in. For example, on Linux you can see your HOME environment variable's contents like this:

    $ echo $HOME

    Environment variables can be useful when you want to override default settings, or when you need to manage new settings that your system has no reason to create on its own. For instance, when you type a command, the only reason your computer knows how to find the application corresponding to that command is that the PATH environment variable tells it where to look. This variable lists valid directories for your operating system to search for commands, whether that command is ls or cp, or a graphical application like Firefox or Chrome, or anything else.

    Different environment variables get used by different systems. Your PATH variable is vital to your terminal emulator, for instance, but a lot less significant to, say, Java (which has its own paths, which point to important Java libraries). However, the USER variable is used by several systems as a way to identify who is requesting a service. For example, if you’re on a multi-user system and need to check your local mailbox, your mail command knows which mail spool to retrieve based on the MAIL and USER variables.

    Setting an Environment Variable

    Usually, the installer program, whether it’s dnf on Fedora, apt on Ubuntu, brew on Mac, or a custom installer, updates your environment variables for a new application. Sometimes, though, when you’re installing something outside of your distribution’s intended toolset, you may have to manage an environment variable yourself. Or you might choose to add an environment variable to suit your preferences. If you decide you want to keep some applications in a bin folder located in your home directory, then you must add that directory to your PATH so your operating system knows to look there for applications to run when you issue a command.

    Temporary Environment Variables

    You can add a location to your path the way you create throw-away variables. It works, but only as long as the shell you used to modify your system path remains open. For instance, open a terminal window and modify your system path:

    pbmac@pbmac-server $ export PATH=$PATH:/home/pbmac/bin

    Confirm the changes - the /home/pbmac/bin has been added at the end of the list:

    pbmac@pbmac-server $ echo $PATH

    Close the terminal window:

    pbmac@pbmac-server $ exit

    Open a new terminal window and take a look at the PATH variable:

    pbmac@pbmac-server $ echo $PATH

    This variable has reverted to its default state because PATH isn’t getting set with each new shell. For that, you must configure your variables to load any time a shell is launched.

    Permanent Environment Variables

    You can set your own persistent environment variables in your shell configuration file, the most common of which is ~/.bashrc. If you’re a system administrator managing several users, you can also set environment variables in a script placed in the /etc/profile.d directory.

    The syntax for setting a variable by configuration file is the same as setting a variable in your shell:

    export PATH=$PATH:/snap/bin:/home/pbmac/bin

    Close the current shell, or else force it to load the updated config. To reload the file, simply enter ". ~/.bashrc" - a period, followed by a space, and ~/.bashrc

    $ . ~/.bashrc

    Finally, take another look at your system path:

    pbmac@pbmac-server $ echo $PATH

    It is now set correctly to include your additional custom directory.

    The export Command

    The export command is a bash shell BUILTINS commands, which means it is part of the shell. It marks environment variables to be exported to child-processes.

    Export is defined in POSIX as the shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to the word.

    In simple terms, environment variables are set when you open a new shell session. If you change any of the variable values, the shell has no way of picking that change. The export command, on the other hand, provides the ability to update the current shell session about the change you made to the exported variable. You don’t have to wait until a new shell session to use the value of the variable you changed.

    Syntax :

    export [-f] [-n] [name[=value] ...] or export -p

    To assign value before exporting use the following syntax:

    pbmac@pbmac-server $ export name[=value]

    An example of using the export command to set vim as a text editor:

    pbmac@pbmac-server $ export EDITOR=/usr/bin/vim
    pbmac@pbmac-server $ export | grep EDITOR
    declare -x EDITOR="/usr/bin/vi"

    Adapted from:
    "Unix shell" by Multiple Contributors, Wikipedia is licensed under CC BY-SA 4.0
    "Using variables in Bash" by Seth Kenlon, is licensed under CC BY-SA 4.0
    "What are environment variables in Bash?" by Seth Kenlon, is licensed under CC BY-SA 4.0
    "export command in Linux with Examples" by Prasoon_Mishra, Geeks for geeks is licensed under CC BY-SA 4.0

    13-A.1: How to Customize the Bash Shell is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?