Bash Environment

Bash Environment

Understanding environment variables and aliases

Table of contents

A Linux environment refers to the overall setup and configuration of a Linux system. It includes:

  • The Linux kernel: The core of the operating system that manages hardware resources and provides common services.

  • The file system hierarchy: The organization of files and directories on the system.

  • Software packages and applications: The various programs and utilities installed on the system.

  • System configuration files: Files that configure how the system works and behaves.

  • Environment variables: Variables that store information about the environment as discussed earlier.

  • Users and groups: The different user accounts and groups on the system.

All of these things together make up the Linux environment.

Users relate to the Linux environment in the following ways:

  • Each user has their own home directory where they store their files.

  • Each user has their own set of environment variables that apply only to them.

  • Each user has their own Bash profile that initialize their specific environment.

  • Users belong to groups that grant them access to specific system resources.

  • Users have different permission levels that determine what they can and cannot do on the system.

  • The root user has full administrative access to manage and configure the entire Linux environment.

So users interact with and have their own personalized view of the overall Linux environment based on their privileges, files, environment settings, and more. The environment is then customized for each individual user to be productive and secure.

In summary, the Linux environment refers to the overall setup of a Linux system, while users interact with and have their own view of that environment based on their privileges, files, and settings.


Variables

Environment Variables: Environment variables store information about the operating system environment. They are accessed using the $ notation, like $HOME and $PATH.

Bash uses environment variables for various purposes:

$HOME - Stores the home directory path of the current user. Bash uses this to access the user's files.

$PATH - Stores a list of directories that Bash searches to find executable commands. This allows you to run commands from any directory.

$PWD - Stores the current working directory. Bash uses this to know the current location in the file system.

$PS1 - Stores the primary prompt string that is displayed when Bash waits for user input. This customizes the look of the command line.

Bash also has many other environment variables for configuring options, setting colors, aliases, etc.

Bash Profiles: Bash profiles are files that are executed whenever a new Bash shell is started. They are used to initialize the shell environment by:

  • Setting environment variables using export

  • Defining aliases using alias

  • Loading functions

  • Setting shell options

The main Bash profiles are:

~/.bash_profile - Executed for login shells
~/.bashrc - Executed for interactive non-login shells ~/.profile - Fallback if .bash_profile is not present

In these files, you can:

export PATH="$PATH:/home/user/bin"
alias ll='ls -l'
function hello() {
  echo "Hello!"
}

When a new terminal is opened, these variables, aliases and functions become available in that shell.

The profiles make it easy to customize your Bash environment with settings that are specific to you and available in every new shell.


Aliases

Aliases are shortcuts that allow you to refer to commands or functions using an alternative name. They are part of the Linux environment in the following ways:

  • Aliases are defined in Bash profiles like .bashrc and .bash_profile. This makes them available in every new shell.

  • Aliases are stored as environment variables, so they are part of the overall Linux environment.

  • Aliases are specific to each user. Each user can define their own aliases that apply only to them.

  • Aliases make the command line more convenient and productive by providing shortcuts.

For example, a common alias is:

alias ll='ls -alF'

This defines an alias "ll" that expands to the "ls -alF" command, which shows all files in long format with indicators.

So now a user can just type:

ll

Instead of the full command:

ls -alF

To get the same result.

Aliases can make complex and frequently used commands easier to run. They become part of the user's personalized Linux environment after defining them in their Bash profiles.


So in summary, Bash environment variables and aliases store information about the OS environment, while Bash profiles initialize that environment whenever a new shell is started. The combination of the two makes Bash highly customizable and productive for each user.