[8-30] BASH environment variable Knowledge grooming

Source: Internet
Author: User

Knowledge Reserve

  • Shell is the interactive command interpreter, the user does not allow direct access to the kernel and operation, then the shell is such a middleware, he is responsible for the user input commands to do grammar analysis, semantic analysis, determine what to do, what enhancements to the options, the operation of what kind of object, And by itself to draw the kernel boot process, is a user and the kernel agent;

  • The broad shell contains the GUI and CLI, whereas the narrow shell only refers to the CLI;

  • Linux supports many shell instances, such as TSH, zsh, and so on, and the most widely used is one instance of bash['s many shells];

  • Note that the shell itself is a command environment, the shell can naturally nest nesting shell, the default CentOS login after the use of bash, if you continue to enter ZSH in the CLI, then you belong to the environment is-->BASH-->ZSH, Please pay attention to your location OH.


Bash Environment Profile Classification

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/72/AD/wKioL1Xrr5fiPjW0AAuI-J5yPb4488.bmp "title=" Bash environment. bmp "alt=" Wkiol1xrr5fipjw0aaui-j5ypb4488.bmp "/>

What is a variable?

    • The variable is essentially a specified area in the memory space, and its data content will change;

    • The variable name essence is a pointer to the memory space of this data change, then in the command execution or operation, only need to pass the variable name, can call that change of data at any time, very convenient

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/AF/wKioL1XrtS6DZyP7AAjaiLUQhrA339.bmp "title=" 1.bmp " alt= "Wkiol1xrts6dzyp7aajailuqhra339.bmp"/>

Types of variables

In Linux, there are roughly 5 types of variables

  • Environment variable: The scope is the current shell process and its child shell process

  • Local variables: Scoped current shell process only

  • Local variable: Only one code fragment in the current shell process is acting [usually a function context]

  • location variables: Shaped like $ $ $ etc, mainly for bash scripts to pass parameters

  • Special variable: $? You can view the execution status of the previous command [ please differentiate between execution results and execution status]

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/72/B1/wKioL1XrvReh6GFxAAGBoG-BhGo168.jpg "title=" Baidushurufa_2015-9-6_12-2-16.png "alt=" Wkiol1xrvreh6gfxaagbog-bhgo168.jpg "/>

How do I define and undo variables?

  • As I said before, Bash is an instance of the shell, which is itself a command environment, and he also contains built-in variables that we can see through #man Bash, and can use/variables to quickly find variables;

  • Since bash is a weak type of programming language, variables can be used without direct use of life, but are empty values;

  • Defining local Variables

    • Name=value

    • Use set to view a defined local variable

  • Defining environment variables

    • Export Name=value

    • Declare-x Name=value

    • Use env, printenv, export to view defined environment variables

  • Undo Variable

    • unset name can be

  • the life cycle of a variable is the current shell process, the process termination variable terminates naturally


How do I reference a variable?

    • ${name} normally {} can be omitted, but sometimes it is not omitted for special purposes, such as the following conditions

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/B4/wKiom1Xrt_qxSdKqAAFKIYKXZ9M236.jpg "title=" Baidushurufa_2015-9-6_11-49-45.png "alt=" Wkiom1xrt_qxsdkqaafkiykxz9m236.jpg "/>

    • It's also true that you want to add an environment variable without overwriting the original variable.

Export path= "$PATH:/usr/test/bin"

    • ' single quote, strong reference, no variable substitution occurs

    • "" Double quotes, weak references, variable substitution occurs


Shell login type and bash configuration file order

There are roughly 2 types of shell logins:

  • interactive login

    • log into the system directly via the console, VTY, SSH and other connection methods in the form of input account password;

    • switch users by #su-l username, note that the-L is in login form

  • Non-interactive Login

    • A terminal window opened under the GUI interface;

    • Execute script: script must specify shell at the beginning, script execution will open the shell automatically

    • Non-parametric switching user via #su username

Bash configuration file Action Order

    • Interactive Login

      • /etc/profile--/etc/profile.d/*.sh--~/.bash_profile-->~/ETC/BASHRC--~/.BASHRC

    • Non-interactive Login

      • ~/.BASHRC--->/ETC/BASHRC--->/etc/profile.d/*.sh


How does the edited configuration file take effect?

    1. Re-login can be;

    2. To have the current shell process reread the configuration file

#Source/path

# . /path

Note: There may be side effects, inadvertently read multiple times will be appended, if you can ensure that only read once, you can ignore this side effect

This article is from the "Blue Warehouse" blog, be sure to keep this source http://bluebox.blog.51cto.com/8852456/1691812

[8-30] BASH environment variable Knowledge grooming

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.