Shell, init files, variables and expansions
Tags: variables, linux, shell, bash
Published: 2022-09-08 06:41:04

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhj2xuimdoh6d94rsnhh.png)
**- /etc/bashrc or /etc/bash.bashrc**: it stores system-wide functions and aliases including other configurations that apply to all system users.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axbtp3kj9893upoyf66c.png)

**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](https://www.youtube.com/watch?v=yIuPu4iLcY4)

> Tip: for more info on this [info](https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html)

---

## #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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sj5kau8zxitwyvpcijt.png)
**Tilde(~) Expansion**: When used at the beginning of a word, it expands into the home directory of the current user.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yiltlt36h8o7jaulq9sb.png)
**Arithmetic Expansion**: a mechanism to perform arithmetic on the shell.
`$((expression))  `

> To see all arithmetic expansion read this [Docum](https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nsyvggn2u3tbtsf4jsy.png) 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26b18omj2jd84zwe1gkg.png)
 **Brace Expansion**: We use the expression in the brace to generate multiple text strings.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jx5515dektnaewy2ta9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8ax5e5r8fvd7zw9sb2p.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2l2rxm25v500qpk1dla5.png)
 
**Parameter Expansion**: the use of variables to expand.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a58nao196t6hjxw38h5r.png)

This are some of the expansions used Linux

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b9i20hwzdtjm0gbkagf.png)

> Tip: for more info on expansion read [article](http://linuxcommand.org/lc3_lts0080.php)

---

## alias

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

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsen3e5zzq9i6fseobqo.png)

> Tip: for more info on alias read [alias](http://www.linfo.org/alias.html) or use the command **man alias**