Once the bash shell starts (or spawn a shell script), you are allowed to create local variables that are visible inside your shell process. You can assign either a string or a numeric value to an environment variable by using the equals sign we assign the value of the variable:<\/p>\n
$ rose_test=testing\r\n$ echo $rose_test\r\ntesting\r\n$<\/pre>\nCongratulations, you just created your first variable. Just remember that any time you need to reference the value of the rose_test environment variable, just reference it by the name $rose_test.
\nIf you need to assign a string of values that contain spaces, you will need to use a single quotation mark to indicate the beginning and end of the string:<\/p>\n
$ rose_test=testing a long string\r\n-bash: a: command not found\r\n$ rose_test='testing a long string'\r\n$ echo $rose_test\r\ntesting a long string<\/pre>\nIf you leave it without the single quotation marks, the bash shell presumes that the next character is another command to process.
\nNote that for the local environment variable I defined, I used lower-case letters, as long as the system environment variables we have seen so far have all used upper-case letters.
\nIn the bash shell, this is a standard convention. If you set up new environment variables, it is not required but recommended to use lower-case letters. This helps to differentiate your personal environment variables from the results of the system environment variables.<\/p>\n
NOTE: It\u2019s extremely important that there are no spaces between the environment variable name, the equal sign, and the value. If you put any spaces in the assignment, the bash shell interprets the value as a separate command:<\/p>\n
$ rose_test2 = test\r\n-bash: rose_test2: command not found\r\n$<\/pre>\nThe set local environment variable within your shell process will be available for use anywhere within your shell process. But, if you spawn another shell, it is not available in the child shell:<\/p>\n
$ bash\r\n$ echo $rose_test\r\n$ exit\r\nexit\r\n$ echo $rose_test\r\ntesting a long string\r\n$<\/pre>\nIn this case, I started a child shell. As you can notice, the rose_test environment variable is not available in the child shell (it contains a blank value). After I left the child shell and returned to the original shell, the local environment variable was still available.<\/p>\n
Similarly, if you set a local environment variable in a child process after you leave the child process, the local environment variable is no longer available:<\/p>\n
$ bash\r\n$ rose_test=testing\r\n$ echo $rose_test\r\ntesting\r\n$ exit\r\nexit\r\n$ echo $rose_test\r\n$<\/pre>\nThe rose_test environment variable set in the child shell doesn\u2019t exist when I go back to the parent shell.<\/p>\n
<\/span>Setting global environment variables<\/span><\/h2>\nThe global environment variables are visible from all processes of the child created by the process that sets the global environment variable. The method used to create a global environment variable is to create a local environment variable and then export it to the global environment.<\/p>\n
This is accomplished by using the export command:<\/p>\n
$ echo $rose_test\r\ntesting a long string\r\n$ export rose_test\r\n$ bash\r\n$ echo $rose_test\r\ntesting a long string\r\n$<\/pre>\nAfter we use the command export on the local environment variable rose_test, we started a new shell process and viewed the value of the rose_test environment variable. This time, the export command made the environment variable global, so it retained its value.<\/p>\n
NOTE: Notice that when you run the command export on a local environment variable, you do not use the dollar sign to reference the variable\u2019s name.<\/p>\n
<\/span>Removing Environment Variables<\/span><\/h2>\nObviously, if you can create a new environment variable, it would make sense that you may also remove an existing environment variable. \u0422his can be done by using the unset command:<\/p>\n
$ echo $rose_test\r\ntesting\r\n$ unset rose_test\r\n$ echo $rose_test\r\n$<\/pre>\nWhen referencing the environment variable within the unset command, keep in mind not to use the dollar sign.
\nWhen dealing with global environment variables, things get a bit tricky. If you\u2019re in a child process and unset a global environment variable, it only applies to the child process. \u0422he global environment variable remains available within the parent process:<\/p>\n
$ rose_test=testing\r\n$ export rose_test\r\n$ bash\r\n$ echo $rose_test\r\ntesting\r\n$ unset rose_test\r\n$ echo $rose_test\r\n$ exit\r\nexit\r\n$ echo $rose_test\r\ntesting\r\n$<\/pre>\nIn this example, we set a local environment variable called rose_test, then exported it to make it a global environment variable. Then we started a child shell process and checked to make sure that the global environment variable rose_test was still available. Next, while still in the child shell, we used the unset command to remove the global environment variable rose_test, then exited the child shell. Now back in the original parent shell, we checked the rose_test environment variable value, and it is still valid.<\/p>\n
<\/span>Default Shell Environment Variables<\/span><\/h2>\nThere are concrete environment variables that bash shell are using to define the system environment. You can always count these variables to be used on your Linux system. Because the bash shell is a derivative of the original Unix Bourne shell, it also includes environment variables originally defined in that shell.<\/p>\n
The next examples are showing that the environment variables the bash shell provides are compatible with the original Unix Bourne shell.
\nSo far the most valuable environment variable in this list is the PATH environment variable.
\nWhen you enter a command in the shell CLI (command line interface), the shell should search the system to find the program. The directories it searches looking for commands are defined by the PATH environment variable. On Linux system, the PATH environment variable should be looking something like this:<\/p>\n
$ echo $PATH\r\n\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/usr\/games:\/usr\/local\/games:\/snap\/bin\r\n$<\/pre>\nIn this case, shows that there are eight directories where the shell looks for commands. Each directory is separated by a colon and there is nothing at the end of the PATH variable also the end of the directory listing. You can always add additional directories to the PATH by adding the new directory and adding another colon. The PATH likewise demonstrates the request in which it searches for orders.
\nIn addition to Bourne’s standard environment variables, the bash shell also provides several variables, as shown:<\/p>\n
<\/span>The bash Shell Bourne Variables:<\/span><\/h2>\nCDPATH: A colon-separated list of directories used as a search path for the cd command.
\nHOME: The current user\u2019s home directory.
\nIFS: A list of characters that separate fields used by the shell to split text strings.
\nMAIL: The filename for the current user\u2019s mailbox. The bash shell checks this file for new mail.
\nMAILPATH: A colon-separated list of multiple filenames for the current user\u2019s mailbox. The bash shell checks each file in this list for new mail.
\nOPTARG: The value of the last option argument processed by the getopts command.
\nOPTIND: The index value of the last option argument processed by the getopts command.
\nPATH: A colon-separated list of directories where the shell looks for commands.
\nPS1: The primary shell command line interface prompt string.
\nPS2: The secondary shell command line interface prompt string.<\/p>\n
<\/span>The bash Shell Environment Variables:<\/span><\/h2>\nBASH: The full pathname to execute the current instance of the bash shell.
\nBASH_ENV: When set, each bash script attempts to execute a startup file defined by this variable before running.
\nBASH_VERSION: The version number of the current instance of the bash shell.
\nBASH_VERSINFO: A variable array that contains the individual major and minor version numbers of the current instance of the bash shell.
\nCOLUMNS: Contains the terminal width of the terminal used for the current instance of the bash shell.
\nCOMP_CWORD: An index into the variable COMP_WORDS, which contains the current cursor position.
\nCOMP_LINE: The current command line.
\nCOMP_POINT: The index of the current cursor position relative to the beginning of the current command.
\nCOMP_WORDS: A variable array that contains the individual words on the current command line.
\nCOMPREPLY: A variable array that contains the possible completion codes generated by a shell function.
\nDIRSTACK: A variable array that contains the current contents of the directory stack.
\nEUID: The effective user ID of the current user.
\nFCEDIT: The default editor used by the fc command.
\nFIGNORE: A colon-separated list of suffixes to ignore when performing filename completion.
\nFUNCNAME: The name of the currently executing shell function.
\nGLOBIGNORE: A list of colon-separated patterns, defining the set of filenames to be ignored by expanding the filename.
\nGROUPS: A variable array containing the list of groups of which the current user is a member.
\nhistchars: Up to three characters which control history expansion.
\nHISTCMD: The history number of the current command.
\nHISTCONTROL: Controls what commands are entered in the shell history list.
\nHISTFILE: The name of the file to save the shell history list (.bash history by default).
\nHISTFILESIZE: The maximum number of lines to save in the history file.
\nHISTIGNORE: A list of pattern separated by a colon used to decide which commands are being ignored for a history file.
\nHISTSIZE: The maximum number of commands stored in the history file.
\nHOSTFILE: Contains the name of the file that should be read when the shell needs to complete a hostname.
\nHOSTNAME: The name of the current host.
\nHOSTTYPE: A string that describes the bash-shell machine works.
\nIGNOREEOF: The number of consecutive EOF characters the shell must receive before exiting. If this value doesn\u2019t exist, the default is one.
\nINPUTRC: The name of the Readline initialization file (the default is .inputrc).
\nLANG: The locale category for the shell.
\nLC_ALL: Overrides the LANG variable, defining a locale category.
\nLC_COLLATE: Sets the collation order used when sorting string values.
\nLC_CTYPE: Determines the interpretation of characters used in filename expansion and pattern matching.
\nLC_MESSAGES: Determines the locale setting used when interpreting double-quoted strings preceded by a dollar sign.
\nLC_NUMERIC: Specifies the local setting used for number formatting.
\nLINENO: The line number in a script currently executing.
\nLINES: Defines the number of lines available on the terminal.
\nMACHTYPE: A string defining the system type in cpu-company-system format
\nMAILCHECK: How often the shell should check (default is 60 seconds) for new mail.
\nOLDPWD: The previous working directory used in the shell.
\nOPTERR: If set to 1, the bash shell displays errors generated by the getopts command.
\nOSTYPE: A string defining the operating system the shell is running on.
\nPIPESTATUS: A variable array containing a list of exit status values from the processes in the foreground process.
\nPOSIXLY_CORRECT: If set, bash starts in POSIX mode.
\nPPID: The process ID (PID) of the bash shell\u2019s parent process.
\nPROMPT_COMMAND: If set, the command to execute before displaying the primary prompt.
\nPS3: The prompt to use the select command.
\nPS4: The prompt displayed before the command line is echoed if the bash -x parameter is used.
\nPWD: The current working directory.
\nRANDOM: Returns a random number between 0 and 32767. Assigning a value to this variable seeds the random number generator.
\nREPLY: The default variable for the read command.
\nSECONDS: The number of seconds since the shell was started. Assigning a value resets the timer to the value.
\nSHELLOPTS: A colon-separated list of enabled bash shell options.
\nSHLVL: Indicates the shell level, incremented by one each time a new bash shell is started.
\nTIMEFORMAT: A format specifying how the shell displays time values.
\nTMOUT: The value of how long (in seconds) the select and read commands should wait for input. The default of zero indicates to wait indefinitely.
\nUID: The real user id of the current user.<\/p>\n
You may notice that not all of the default environment variables are shown after we used the set command. The explanation for this can be that though these are the default environment variables, not all of them are required to contain a value.<\/p>\n