DEV Community

Cover image for Shell, init files, variables and expansions
Haile Melaku
Haile Melaku

Posted on • Updated on

Shell, init files, variables and expansions

So, today am going to be checking more about shell, more specifically topics on Shell initialization files,variables and shell expansions.


#Shell initialization files

When a shell is opened, there are certain initialization/startup files that are read which help to setup an environment for the shell itself and the system user.

Before we get into shell initialization we need to understand the three mods of the shell when it is opened:

  • Interactive login

  • Interactive non-login

  • non-interactive non-login shell

  • non-interactive login shell

Now let's see each one in more detail

  • Login shell: executes under our user ID when we log in to a session.

  • Interactive shell: Reads commands from user input on a terminal.

Interactive Login

In this mode you login in a remote computer.

for example it can be run through:

  • using ssh

Interactive non-login

In this mode you just open a terminal.

for example it can be run through:

  • running /bin/bash
  • running /bin/su

non-interactive non-login shell

In this mode you run a script, it only opens to execute the script and closes immediately once the script is finished.

non-interactive login shell

In this mode you run a script remotely vi ssh or telnet.

for example :
echo command | ssh server


Now that we understood the concepts of intractability and login ,we need to see the two types of shell initialization files

  • system-wide startup files

  • user-specific startup files

system-wide startup files: are global configuration that apply to all users in the system and are located in the /etc directory.

Some of them are :

- /etc/profiles: it stores system-wide environment configurations and startup programs for login setup.

On some systems this file holds pointers to other configuration files such as:

  • /etc/inputrc: to configure the command line bell-style.
  • /etc/profile.d directory: contain system-wide behavior of specific program configuration

Image description
- /etc/bashrc or /etc/bash.bashrc: it stores system-wide functions and aliases including other configurations that apply to all system users.

Image description

wuser-specific startup files: are store configurations that apply to a single user on the system and located in the users home directory as dot files.

Some of them in there process are :

  • .bash_profile : stores user specific environment and startup programs configurations.

  • .bashrc : Stores user-specific aliases and functions

  • .bash_login : Contains specific configurations that are normally only executed when you log in to the system. When the ~/.bash_profile is absent, this file will be read by bash

  • .profiles : In the absence of ~/.bash_profile and ~/.bash_login, ~/.profile is read. It can hold the same configurations, which are then also accessible by other shells.

  • .bash_history : Bash maintains a history of commands that have been entered by a user on the system. This list of commands is kept in a user’s home directory in the ~/.bash_history file.

  • .bash_logout : it’s not used for shell startup, but stores user specific instructions for the logout procedure. It is read and executed when a user exits from an interactive login shell.

Tip: to see the login process use this Video

Tip: for more info on this info


#Variables

A variable is an abstract storage location paired with an associated symbolic name.

A shell variable is a variable that is available only to the current shell.

An environment variable is available system wide and can be used by other applications on the system.

Bash keeps a list of two types of variables:

  • Global variables

  • Local variables

Global variables: are variables that are available to all Bash scripts on your system, we use the commands env or printenv to display environment variables.

Local variables: are variables that can only be used within the script (or shell) in which they're defined..

To create a shell variable we use the following command

NAME='VALUE'

export : we can use the export command to export variables to the child-processes.

to view all the exported variables ,we use the command

export -p

And to export the variables, we use the command

export NAME='VALUE'

Reserved variable are set or used by Bash, but other shells do not normally treat them specially.

Some examples of reserved variable are:

HOME : The current user's home directory; the default for the cd built-in. The value of this variable is also used by tilde expansion.

PATH : A colon-separated list of directories in which the shell looks for commands.

PS1 : The primary prompt string. The default value is "'\s-\v\$ '".


#Expansion

Expansion is the process of Performed upon the text before it carries out our command.

There are many kinds of Expansions performed, some of them are:

Pathname Expansion: is mechanism by which wildcards work.

Image description
Tilde(~) Expansion: When used at the beginning of a word, it expands into the home directory of the current user.

Image description
Arithmetic Expansion: a mechanism to perform arithmetic on the shell.
$((expression))

To see all arithmetic expansion read this Docum

Image description

Image description
Brace Expansion: We use the expression in the brace to generate multiple text strings.

Image description

Image description

Image description

Parameter Expansion: the use of variables to expand.

Image description

This are some of the expansions used Linux

Command Substitution: is able to output a command as an expansion.

Image description

Tip: for more info on expansion read article


alias

alias is a linux command used to create custom made linux commands.

alias [alias-name[=string]...]

Image description

Tip: for more info on alias read alias or use the command man alias

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

When you talk about "global variables" being available in "all shells" if you use export, that's not really the case. You can open two terminals, for example, and export whateve r you like - it won't appear in the other one's environment. Using export only makes environment variables available to _sub_shells.

Collapse
 
haile08 profile image
Haile Melaku

Hey, saw the review you suggested and you are right. when opening two terminals and using export on one of them, will only affect the child process of the one and it wont affect the other one.

Thanks for the review :)