Basic Linux TutorialIntroduction – What is it? Why to learn? Linux installation directory structures Boot process Run levels in Linux Desktop Environments Different shells BASH Internal and External Commands Basic Linux Commands Important files and directories in Linux Environmental and Shell Variables Command history in Linux Character classes in Linux Text editors vim nano Searching files Creating new files Viewing File Contents File commands File permissions and ownership WildCards (Globbing) in files File compression Directory commands xargs command in Linux Comparing files Searching patterns using grep command Translating the characters using tr command Extracting data using cut command Stream editing using sed command Data extraction and reporting using awk command Sorting the file or string input uniq command in Linux Difference between grep, tr, cut, sed and awk commands Hardware commands Hard disk and memory space commands Working with Processes Managing Jobs Working with cron jobs Service command in Linux Network commands Managing Users and Groups Other Popular commands Standard streams and Redirection Pipes Package Managers in Linux
Variables in linux shellVariables are key-value pairs which are used by different processes in Linux. There are 4 types of variables.
- Temporary local Shell variables (Only available in current shell)
- Temporary Shell Environment variables (available in current shell, processes spawned by current shell, and child shells)
- Permanent User Environment Variables (stored and exported in .bashrc, .bash_profile, .bash_login, .profile)
- System environment permanent variables (stored in /etc/environment, /etc/profile, /etc/profile.d/, /etc/bash.bashrc)
Creating temporary local shell variablesTo create local shell variable, you can execute below command. Here we are creating variables with name “var1” and it’s value is “value1”.
set command can be used to see all local variables as well as environment variables.
Creating temporary environment variablesTo create Global shell variable, you can execute below command. This variable can be accessed in child processes and child shells.
Below image shows how you can create your own variables and then convert them into global variables. In below example, we have created new variable called as “SAGAR” and assigned it the value as “Hello”. This is a temporary variable (Also called as Session Variable). To convert it into temporary environment variable, we have used export command. Note that exported variables are only available in child processes. Also next time if you open terminal and view the content of any exported variables, you will see nothing. To make the change permanent, you have to put that variable in .bashrc file under your home directory.
Creating Environment VariablesGlobal shell variables can be accessed in child shells and processes. But the problem is that these variables will be erased as soon as we close current shell terminal. That’s when environment variables come into picture. To convert the exported global variables into environment variables, you need to store below command in .bashrc or your login profile file. So that every time you login or open the shell, those variables can be accessed
This is called as user defined environment variables. But there are many built-in environment variables that you can access. Here is the list of some of the system built-in environment variables.
echo $BASH – displays the path of bash shell. echo $HOME – displays the home directory of current user. echo $OSTYPE – displays the OS type. echo $SHELL – displays current shell type. echo $PWD – displays present working directory. echo $PATH – displays contents of the PATH variable.
Permanent User Environment VariablesYou can view all the environment variables by using below command.
or you can also use “env” command to print the environment variables. Note that these command will not show local shell variables. Sample output of above command is given below.
To view specific variable value, you can use any of the below commands.
echo $HOME printenv HOME
Editing Environment VariablesYou can edit the environment variables by using below command. Editing the variable is very simple. Just set the new value for the variable at command prompt. Remember that changes made to the environment variables within shell are temporary. For example – If you want to change the variable called PWD, you can do it using below command.
PWD=”new directory name”
Deleting the Environment VariablesYou can delete the variables by using unset command.
Special variables in Linux
- $# – This displays total number of command line arguments.
- $* and [email protected] – This displays all arguments to the shell.
- $$ – This displays the process id of current shell.
- $! – This displays the process id of the last background process started with & symbol.
- $0, $1, $2 and so on – $0 is the name of the command running at the moment. $1 is the first argument to the command. $2 is the second argument to the command and so on.
- Storing environment variables
- ~/.bashrc – This is user specific file. This file is sourced every time you open new bash shell.
- ~/.bash_profile – This is user specific file.
- ~/.bash_login – This is user specific file. Variables are initialized from this file at the start of login shell.
- /etc/environment – Variables stored in this file are available across system and for every user. Also these variables will be available local as well remote sessions.
- /etc/profile – Variables in this file will not be accessible for local login session.
- /etc/bash.bashrc – Variables are available for local user sessions.
$ man bash
Complex problems, Simple Solutions