Chapter 5. Customizing the Environment

In a default system, we get certain settings that are preconfigured. As time progresses, we often feel the need to modify some of the default settings provided. Similar needs arise when we are working in a shell to get things done, for example, modifying the environment according to the needs of the application. Some of the features are so irresistible that we may need them every time, for example, the editor of our choice used by an application. While working on an important task, it may happen that we forget a command that we used a few days ago. In such cases, we try to recall that command as soon as possible to get work done. If we can't remember, we consume time and effort searching on the Internet or in text books for the exact command and syntax.

In this chapter, we will see how, by adding or changing the existing environment variables, we can modify the environment as per our application needs. We will also see how a user can modify the .bashrc, .bash_profile, and .bash_logout files to make the setting changes available permanently. We will see how we can search and modify the history of previously executed commands. We will also see how to run multiple tasks from a single shell and manage them together.

This chapter will cover the following topics in detail:

  • Knowing the default environment
  • Modifying the shell environment
  • Using bash startup files
  • Knowing your history
  • Managing tasks

Knowing the default environment

Setting up a proper environment is very important for running a process. An environment consists of environment variables that may or may not have a default value set. The required environment is set by modifying the existing environment variables or creating new environment variables. Environment variables are exported variables that are available to the current process and also its child processes. In Chapter 1, The Beginning of the Scripting Journey, we learned about some of the builtin shell variables that can be used in our application as environment variables to set the environment.

Viewing a shell environment

To view the current environment in the shell, we can use the printenv or env commands. Environment variables may have no value, a single value, or a multiple value set. If multiple values exist, each value is separated by a colon (:).

printenv

We can use printenv to print the value associated with a given environment variable. The syntax is as follows:

$ printenv [VARIABLE]

Consider the following as examples:

$ printenv SHELL    # Prints which shell is being used
/bin/bash
$ printenv PWD    # Present working directory
/home/foo/Documents
$ printenv HOME    # Prints user's home directory
/home/foo
$ printenv PATH    # Path where command to be executed is searched
/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/home/foo
$ printenv USER HOSTNAME  # Prints value of both environment variables
foo
localhost

If no VARIABLE is specified, printenv prints all environment variables as follows:

$ printenv  # Prints all environment variables available to current shell

env

We can also use the env command to view environment variables as follows:

$ env

This displays all environment variables defined for a given shell.

Note

To view value(s) of a specific environment variable, the echo command can also be used followed by an environment variable name prefixed with a dollar symbol ($). For example, echo $SHELL.

Differences between shell and environment variables

Both shell and environment variables are variables that are accessible and set for a given shell that may be used by an application or a command running in that shell. However, there are a few differences between them, which are set out in the following table:

Shell variables

Environment variables

Both local and exported variables are shell variables

Exported shell variables are environment variables

The set builtin command is used to see the name and corresponding value of a shell variable

The env or printenv command is used to see the name and corresponding value of an environment variable

Local shell variables are not available for use by their child shells

Child shells inherit all environment variables present in the parent shell

A shell variable is created by specifying a variable name on the left and value(s) separated by a colon (:) on the right-hand side of an equal operator (=)

An environment variable can be created by prefixing an export shell built - in command to the existing shell variable, or while creating a new shell variable

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.165.82