The difference between the set, Env, declare, and export display shell variables in Linux

Source: Internet
Author: User
Tags aliases builtin domain name server

The difference between the set, Env, declare, and export display shell variables in Linux

The shell variable consists of two variables

1. Shell Local Variables

Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
The variables defined by the assignment statement can be defined by the following methods

A1="1234"delcare A2="2345"
2. User's environment variables

All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
The shell private variables exported through the export syntax can be exported using the following method to export the user environment variables

A1="1234"export A1  #先定义再导出export A3="34"
Show Shell variables

Env This is a tool, or a Linux command, that displays the user's environment variables.
Set displays the user's local variables and user environment variables.
Export Displays the shell local variables that are exported as user variables, and displays the variables ' properties, that is, the variables that are exported to the environment variables by local variables (such as exporting WWC an environment variable or exporting an environment variable through declare-x LCY)
Declare displays the user's shell variables (local variables and environment variables) as set

DECLARE command

The declare command is used to declare shell variables.
declare WWC="wangwenchao"
You can also omit declare
WWC="wangwenchao"

Usage
Declare [+/-][rxi][variable name = set value] or declare-f

    1. +/-"-" can be used to specify the properties of the variable, and "+" is the property set by the cancellation variable.
    2. -F displays only functions.
    3. R sets the variable to read-only.
    4. The variable specified by x becomes an environment variable that can be used by a program other than the shell.
    5. I [Set value] can be a numeric value, a string, or an expression.
Export command

The Export command is used to set or display environment variables. Used to export local variables to environment variables.

Usage
Export [-fnp][variable name]=[variable setting value]

    1. -F represents the function name in [variable name].
    2. -n Deletes the specified variable. The variables are not actually deleted, but are not exported to the execution environment of the subsequent directives.
    3. -p lists all the environment variables that the shell assigns to the program.

The function of **ps:export command is coincident with declare function part; export wwc= "Wangwenchao" equals Declare-x wwc= "Wangwenchao"; function Export-n WWC equivalent to declare +x WWC; Function Export view exported environment variables

SOURCE command

SOURCE Command usage:
SOURCE FileName
Function: Reads and executes the commands in the filename under the current bash environment.
Note: This command usually uses the command "." To replace.
such as: source. BASH_RC and. The. BASH_RC is equivalent.

The difference between the source command and shell scripts is that source executes the command in the current bash environment, and scripts starts a child shell to execute the command. This way, if the command to set the environment variable (or alias, etc.) is written into scripts, it will only affect the child shell and cannot change the current bash, so use the source command when setting the environment variable through the file (command column).

The biggest difference between source and./xxx.sh or bash xxx.sh is that the variable set in the file is visible to the current shell, and the variable set by the latter is invisible to the current shell. You know./xxx.sh and bash xxx.sh are all executed in the child shell of the current shell, and the variables in the child shell do not affect the parent shell, and source reads the commands in the file and executes them, all of which are set in the parent shell.

Nohup command

The Nohup command is no hanp up an abbreviation.
Unix/linux generally want to let a program run in the background, many use & at the end of the program to let the program run automatically, but if you want to exit the terminal, the program is still running in the background, you have to use Nohup and & combination to achieve.

nohupUsage
Nohup Command [Arg ...] [&]

The nohup command runs commands specified by the command parameter and any related arg parameters, ignoring all pending (SIGHUP) signals, or modifying the process specified with the-P option to ignore all pending (SIGHUP) signals (Close the terminal, log off the user, etc., but note Ctrl + C is not a hang signal).
You can also use the NOHUP command to run programs in the background after you log off. To run the Nohup command in the background, add & (the symbol representing "and") to the end of the command. The output is appended to the Nohup.out file in the current directory .

Note:-p PID and Command cannot be specified together.
When using the-p PID, the output of the specified process is not redirected to Nohup.out.
ctrl + cnohup bash wwc.shwill stop running, but bash wwc.sh & will run normally nohup bash wwc.sh , and the terminal will run normally and bash wwc.sh & will stop, so use Nohup with &.

Kill a process executed by nohup

grep commond | grep -v grep  例如:ps -ef | grep "bash wwc.sh" | grep -v grep

Find Process ID
Kill-9 ID

Reference Link:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.cmds4/nohup.htm

Type command

The type command is used to display the types of the specified command, judging whether the given instruction is an internal or external instruction.

Usage

    1. -T: Output "file", "Alias" or "builtin", respectively, indicating that the given instruction is "external instruction", "command alias" or "internal instruction";
    2. -P: If the given instruction is an external instruction, its absolute path is displayed;
    3. -A: Displays information for a given instruction, including command aliases, in the path specified by the environment variable "path".

Results
Alias: Aliases.
Keyword: key word, shell reserved word.
Function: Functions, shell functions.
Builtin: Built-in command, Shell built-in command.
File: Files, disk files, external commands.
Unfound: Not found.

Built-in commands and external commands

Built-in commands
Internal commands are actually part of the shell program, which contains simple Linux system commands that are recognized by the shell and run inside the shell program. Typically, the shell is loaded and resides in system memory when the Linux system loads and runs. Internal commands are written in the Bashy source code, which executes faster than external commands because parsing the internal command shell does not require the creation of child processes. such as: Exit,history,cd,echo and so on.
Some commands are built for their necessity , such as a CD used to change the directory, and read will pass the input data from the user (and file) to the shell for external light.
Another built-in command exists for efficiency , the most typical of which is the test command, which is often used when scripting. There are also I/O commands, such as echo in printf.

External command
External commands are part of the utilities in a Linux system, because the utility is usually powerful, so it contains a large number of programs that are not loaded into memory with the system when the system is loaded, but instead call memory when needed. Entities that are normally external commands are not included in the shell, but their command execution procedures are controlled by the shell. The shell program manages the path lookup, loading and storage of external command execution, and controls the execution of the command. External commands are installed outside of bash and are usually placed in/bin,/usr/bin,/sbin,/usr/sbin ... Wait a minute. The "Echo $PATH" command allows you to view the storage path for external commands, such as LS, VI, and so on.

The external command is the command executed by the shell copy (the new process), and the basic process is as follows:

    1. To establish a new process. This process is a copy of the shell.
    2. In the new process, look for specific commands within the directory listed in the PATH variable.
      /bin:/usr/bin:/usr/x11r6/bin:/usr/local/bin is a typical default value for the PATH variable.
      When the command name contains a slash (/) symbol, the path lookup step is skipped.
    3. In the new process, replace the executing shell program with the new program found and execute it.
    4. Once the program is complete, the original shell will then read the next command from the terminal and execute the next command in the script.

After the user enters a command at the command line, the shell typically fork and exec the command, but the shell's built-in command is the equivalent of invoking a function in the shell process and does not create a new process.
PS: In order to achieve a powerful function, some built-in commands also have an external command implementation version, these external commands are often more powerful than the corresponding internal commands, but the relative efficiency may be low (e.g. CD, ECHO, etc.), and some external commands in order to perform the issue of effectiveness, There are implementations in the built-in commands (for example: printf), if a command has both an internal implementation and an external implementation, the direct use of its internal version, to use its external implementation, you can use the absolute path, for example: Echo is used by its internal command,/bin/echo is used by its external command To view the path to an external command, you can use the which command, such aswhich echo

Set environment variable permanently valid and temporary valid permanently valid for all users

/etc/profile is the environment variable settings file that runs every time a user logs on, and the file is executed when the user logs on for the first time. Valid for all users.
An example:

# System-wide .profile for sh(1)if [ -x /usr/libexec/path_helper ]; then    eval `/usr/libexec/path_helper -s`fiif [ "${BASH-no}" != "no" ]; then [ -r /etc/bashrc ] && . /etc/bashrcfiexport THEOS=/opt/theosexport WWC=wangwenchao_lcy
Valid for current user

You can add the environment variables you want to add to your home directory. Profile or. bash_profile, if the file exists, add it, and if the file does not exist, create one in the user's home directory.

Summarize:

    1. /etc/profile (it is not recommended to modify this file)
      A global (public) configuration that reads the file when logged on, regardless of the user.

    2. /ETC/BASHRC (typically add system-level environment variables to this file)
      Global (public) configuration, which will be read by the bash shell, regardless of the way it is executed. You can set the system prompt PS1 and so on. For example ps1=\h:\w \u$, then the format of the prompt is: Host: Current directory user name $

    3. ~/.bash_profile or ~/.profile (typically add user-level environment variables to this file)
      Each user can use the file to enter shell information dedicated to their own use, and when the user logs in, the file executes only once!

PS:MAC environment variables are loaded in order /etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.profile , and these files ~/.bash_profile ~/.bash_login ~/.profile are mutually exclusive relationships, if the previous file exists, the subsequent file will not be loaded

Other profile/etc/paths (globally recommended to modify this file)

Edit paths, set the PATH environment variable

/etc/hosts

Host name and IP configuration file
Hosts file is a Linux system responsible for the rapid resolution of IP address and domain name files, in ASCII format in the "/etc" directory, the file name "hosts" (different versions of Linux, this configuration file may also be different.) For example, Debian's counterpart file is/etc/hostname). The Hosts file contains mappings between IP addresses and host names, and also includes aliases for host names. In the absence of a domain name server, all network programs on the system can query the file to resolve the IP address corresponding to a host name, otherwise it will need to use the DNS service program to resolve. You can often add common domain names and IP address mappings to the Hosts file for quick and easy access.

The Hosts file has the following format:
IP address host name/domain name host alias (optional)

The first part: Network IP address;
Part Two: hostname or domain name;
The third part: hostname alias;

System directory usr directory

USR is the abbreviation for UNIX system resources

The former/USR is also the user's home directory, storing a variety of user files-now has been replaced by/home (for example,/usr/someone has been changed to/home/someone). Modern/usr only stores a variety of programs and data, the user directory has been transferred. Although the/USR name has not changed, its meaning has changed from "User directory" to "Unix system resources" directory. It is worth noting that on some UNIX systems,/usr/someone is still treated as a user's home directory, such as Minix.

The difference between/sbin,/bin,/usr/sbin and/usr/bin

/sbin mainly put some system management prerequisite programs, such as reboot, shutdown, etc.,/sbin commands belong to basic system commands

/bin store Some common basic commands, such as Ls,chmod, which are often used in configuration file scripts in Linux systems.

/usr/sbin put some user-installed system management programs, store some of the non-mandatory system commands

/usr/bin Store some user commands

Summary: If this is a user-and administrator-required binary file, it will be placed in/bin. If this is a prerequisite for the system administrator, but the generic user does not use the binaries, it will be placed in the/sbin. Relatively. If it is not a user-required binary, it will probably be placed in/usr/bin, and if it is not a necessary tool for the system administrator, it will probably be placed in/usr/sbin.

UNIX directory structure from a historical perspective

In 1969, Ken Thompson and Dennis Ritchie invented Unix on minicomputer PDP-7. In 1971, they upgraded the mainframe to PDP-11.
It wasn't long before the operating system (the root directory) became larger and bigger, and a disk was not fit. As a result, they added a second set of RK05, and stipulated that the first disk specifically put the System program, the second disk dedicated to the user's own program, so mount Directory point named/usr. That is, the root directory "/" is mounted on the first disk, and the "/usr" directory is mounted on the second disk. In addition, the directory structure of the two disks is identical, the first disk directory (/bin,/sbin,/lib,/tmp ... ) appears again in the/usr directory.
Later, the second plate is full, they have to add a third set of RK05, the directory is mounted to the name of home/home, and specify/usr for storing the user's program,/home for storing the user's data.
Since then, this directory structure has continued. As hard disk capacity becomes larger, the meanings of each directory are further clarified.

    1. /: Storage System program, which is the UNIX program developed by T/T.
    2. /usr: programs developed by UNIX system vendors, such as IBM and HP.
    3. /usr/local: Store the user's own installed program.
    4. /OPT: In some systems, it is used to store programs developed by third-party vendors, so it is named option, meaning "optional".

/usr/bin is used for distribution Manager (such as Ubuntu apt, etc.) to store the path of the application it manages,/usr/sbin relationship with/usr/bin similar to/bin and/sbin
/usr/local/bin is used to store users ' own programs (such as their own compiled packages), and is not controlled by the bundle manager. If the user puts their own program under/usr/bin, it is possible to modify, delete, or overwrite the file with the same name in the future by the package Manager.

Usually the/usr/bin below are pre-installed executable programs that change as the system upgrades
/usr/local/bin Directory is a place for users to place their own executable program, it is recommended to put here, will not be upgraded by the system to overwrite the same name file

General environment variable path is set to/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin, note the order of the problem, if the previous directory found the command, it will no longer search the back of the directory.

Reference Links: The origin of UNIX directory structure--Nanyi's blog

Problem

Load order of Linux environment variables?

The difference between the set, Env, declare, and export display shell variables in Linux

Related Article

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.