Linux command line--using Linux environment variables

Source: Internet
Author: User
Tags aliases

5.1 What time environment variables

The bash shell uses a feature that becomes an environment variable to store information about the shell response and the work environment, which is why they become environment variables. It allows you to store data in memory in order to run the features of your account, system, shell, and any other data you need to store

    • Classification of environment variables
      • Global variables
      • Local variables
5.1.1 Global Environment variables
    • Global environment variables are visible not only to shell sessions, but also to all shell-created child processes, and local variables are only visible to their shells that are created
      • This is useful to global environment variables for programs that need to get parent process information in those sub-processes
    • System environment variables all use uppercase letters to differentiate the environment variables of ordinary users
    • PRINTENV: Viewing Global environment variables
    • echo + $ environment variable name: View individual environment variable values
5.1.2 Local Environment variables
    • Local variables are visible only in the process in which they are defined
    • Set command: Displays all environment variables that are set for a particular process, including global environment variables and local environment variables
$ set .BASH=/bin/bashEUID=500IFS=$‘ \t\n‘...
    • 1
    • 2
    • 3
    • 4
    • 5
5.2 Setting environment Variables 5.2.1 setting Local environment variables

Once you open the bash shell (or execute a shell script), you can create local variables that are visible within the shell process.

    • A local variable can be set by an equal sign, the value of which can be set when a number or a string
$ test=testing$ echo $testtesting$
    • 1
    • 2
    • 3
    • 4
    • If you want to assign a string value that contains a space to a variable, you must use single quotation marks to define the beginning and end of the string.
$ test=‘testing a long string‘$ echo $testtesting a long string$
    • 1
    • 2
    • 3
    • 4
    • Local variables generally use lowercase letters
    • Note: There is no space between the environment variable name, equal sign, and value
    • Once the environment variable is set, it can be used in the shell process, and if the environment variable is set in the child process, the local variable cannot be used once the child process is exited.
5.2.2 Setting Global environment variables
    • How to set a Global environment variable: first set a local variable and then export it to the global environment
      • To complete the export task by exporting command
$echo $testtesting a long string$export test$bash$echo $testtesting a long string$
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
Delete environment variables
    • unset command: Delete an environment variable
    • Usage: unset + environment variable name
      • Note Do not add the $ symbol
unset test$ echo test$
    • 1
    • 2
    • 3
    • 4
    • Note: If all variables are removed only in the child process, only the sub-process is valid and remains valid in the parent process
5.4 Default shell environment variables

By default, bash shell uses specific environment variables to define system environment variables
The bash shell is derived from the Unix Bourne shell, which retains the environment variables defined in the UNIX Bourne shell.

    • Common Shell-supported Bourne environment variables

      • Home: The current user's home directory
      • Ifs:shell a column of characters used to separate text strings
      • PATH: Colon-delimited list of Shell Lookup command directories, showing the order of Shell lookup commands
      • Ps1:shell main prompt for the command line interface
      • Ps2:shell the command line interface's secondary prompt
    • Not all default environment variables are listed when the SET command is run, although these are the default environment variables, but not all of them must have a value

5.5 Setting the PATH environment variable

The PATH environment variable defines the search path for the command line Input command, which generates an error if it is not found

$ myprog-bash: myprog: command not found$
    • 1
    • 2
    • 3
    • Ways to solve the above problems
      • Ensure that the PATH environment variable contains all the directories where the application resides
      • Add a new search directory to an existing PATH environment variable without having to define it from scratch
      • The original path value is applied, and then a new directory is added to the string
PATH=$PATH:/home/user/test
    • 1
    • You usually add a single dot to the PATH environment variable.
5.6 Positioning System Environment variables
    • 3 Ways to Start bash shell
      • Log on when the default login shell
      • Interactive Shell as a non-login shell
      • As a non-interactive shell that runs scripts
5.6.1 Login Shell
    • When you log in to a Linux system, the bash shell starts as a login shell
    • The logged-in shell reads commands from 4 different boot files, and the following is the order in which the bash shell processes these files
      • /etc/profile
      • $HOME/.bash_profile
      • $HOME/.bash_login
      • $HOME/.profile
1./etc/profile file
    • This file is the default bash Shell master boot file on the system, as long as the Linux system is logged on, Bash executes the commands in that file
      -Different versions of the Linux distribution file contain different commands
    • There is a last line in the file: Export PATH USER LOGNAME MAIL HOSTNAME ... : These environment variables are guaranteed to be valid in all child processes that are created by the shell
    • The file also has a complex feature: A For statement that accesses each file in the/ETC/PROFILE.D directory one by one
      • It provides a place for Linux systems to centrally store the application-specific startup files that users log in to perform.
      • Most apps will create two boot files
        • One for bash shell (using the. sh extension)
        • One for C shell (using the. csh extension)
    • The lang.csh and. lang.sh files attempt to determine the default set of language literals used on the system, and then set the lang environment variable correctly
2. startup files in the $HOME directory
    • The remaining 3 boot files all play the same role: provide a user-specific startup file to define user-specific environment variables
      • These 3 boot files are all beginning with a point and are hidden files
      • They are in the user's home directory, so each user can edit these files and add their own environment variables to give them the start of each bash shell session with
    • . bash_forfile Startup file role:
      • Check to see if there is a boot file called. BASHRC in the home directory, and then execute the command inside it first.
      • Next, the startup file adds a directory to the PATH environment variable, and in the home directory provides a common place to place the executable file
Interactive shell

If the shell that starts is not the shell that is started when you log on to the system, the shell is an interactive shell

    • If the bash shell is an interactive shell, he does not go back to accessing the/etc/profile file, but will go to the user's home directory to check. BASHRC exists
      • Two roles for the. bashrc file:
        • View common BASHRC files in the/etc directory
          • The generic/ETC/BASHRC boot file is executed by each user who initiates the interactive shell session on the system file
        • Provides a place for users to customize their own command aliases and private script functions
    • The default/ETC/BASHRC file sets some environment variables, but does not execute the export command to make them global
    • The interactive Shell's startup file will only run every time a new interactive shell is launched, so any child shell will automatically execute this interactive shell's startup file
5.6.3 Non-interactive shell

The system executes the shell script with a non-interactive shell, without worrying that it does not have a command line prompt
However, you run a specific startup command every time you run a script on the system

    • Bash shell provides BASH_ENV environment variables to handle this situation
      • When the shell launches a non-interactive shell process, it checks the environment variable to see which startup file to execute, and if so, the shell executes the command in the file.
      • In Linux distributions, this environment variable is not set by default
Variable group
    • Environment variables can be used as arrays, arrays are able to store multiple worthwhile variables
      • This value can be referenced by a single value or an entire array
    • To set multiple values for multiple environment variables, you can place values in parentheses, separated by spaces between values and values
$ mytest=(one two three four five)$
    • 1
    • 2
    • To reference a single array element, you need to use a numeric index value that represents its position in the array, with the values enclosed in parentheses.
$ echo $(mytest[2])three$
    • 1
    • 2
    • 3
    • To display the entire array variable, use the asterisk as a wildcard to place the index value
$ echo ${mytest[*]}one two three four five$
    • 1
    • 2
    • 3
    • Change the value of an index value position
$mytest[2]=seven$echo ${mytest[*]}one two seven four five$
    • 1
    • 2
    • 3
    • 4
    • To delete an element in an array: unset, but note some issues
unset mytest[2]$ echo ${mttest[*]}one two four five$ echo ${mytest[2]}$ echo ${mytest[3]}four$
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Delete entire array: unset + array Name
unset mytest$ echo ${mytest[*]}$
    • 1
    • 2
    • 3
    • 4
5.8 Using command aliases
    • Command aliases allow you to create an alias for common commands (along with their parameters)
    • View an existing alias list: Alias-p
    • Define your own user alias with Alias, once you have defined the alias, you can use it at any time in the shell, including shell scripts
$ alias li=‘ls -il‘$ li...$
    • 1
    • 2
    • 3
    • 4
      • The behavior of a command alias is similar to defining a local environment variable, only valid in the shell process that defines it
        • WORKAROUND: Defined in the $HOME/,BASHRC startup file because the Bash shell always reads this file as an interactive shell

Linux command line--using the Linux environment variable (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.