Programmer self-cultivation-Linux environment variable principle and shell

Source: Internet
Author: User
Programmer self-cultivation & mdash; Linux environment variable principle and shell Overview: Working in linux, this often happens: 1) run a command, -bash: [commandname]: commandnotfound2): execute an ELF executable file: xyz @ hnode02 :~... Programmer self-cultivation-Linux environment variable principle and shell Overview: Working in linux, this often happens: 1) run a command and prompt-bash: [commandname]: command not found2) executes an ELF executable file: xyz @ hnode02 :~ /Downloads/software/bean/workdir>. /bean.exe. /bean.exe: error while loading shared libraries: libTreePlayer. so: cannot openshared object file: No such file or directory when cannotfound, nosuch file, cannotopen, it is often because the related environment variables are not effectively set (or it may be because the related libraries or programs are not installed ). This article mainly explains the essential principles, setup methods, and solutions of environment variables. 1. what is the environment variable 1.1? the environment variable is the parameter set by the system or software. the user environment variable is that after the user logs on to the system, there is a dedicated runtime environment. After you log on to the shell environment, the shell will automatically read/etc/profile ~ /. Profile ~ /. Bashrc/. bash_profile and other files. these files often contain related environment variable settings and initialization. The environment table (pointer array pointing to the actual name = value string) and environment string are usually stored at the top of the process bucket (on the top of the stack). Therefore, the child process inherits the environment variable of the parent process. The problem is: why does the environment variable take effect after I set the relevant environment variable in the command line, but restarts a terminal? In linux, three system calls are provided to modify the environment variables: # include Intputenv (char * str); int setenv (const char * name, const char * value, int rewrite); int unsetenv (const char * name ); 1.2 Common Environment variables and their functions PATH determine the directories to which shell will look for commands or programs HOME Current User HOME directory HISTSIZE number of historical records LOGNAME current user login HOSTNAME refers to the host name before SHELL environment variables related to the wide GE language of the Shell type, in multiple languages, you can modify this environment variable to MAIL the current user's MAIL storage directory PS1 BASIC prompt, for root users is #, for common users is $ PS2 affiliated prompt, the default value is ">" LD_LIBRARY_PATH. the paths above the dynamic link library are system environment variables, which are usually set in non-user-related files. Generally, the user environment variables are in upper case, in the user-related directory. 2. how to set and view the environment variable 2.1 set a new environment variable: $ export HELLO = "Hello! "$ Echo $ HELLOHello! 2.2 View environment variables: Display the environment variable HOME $ echo $ HOME/home/redbooks 2. 3. use the env command to display all environment variables $ env HOSTNAME = redbooks.safe.org PVM_RSH =/usr/bin/rsh Shell =/bin/bash TERM = xterm HISTSIZE = 1000 2. 4. use the set command to display all locally defined Shell variables $ set BASH =/bin/bash BASH_VERSINFO = ([0] = "2" [1] = "05b" [2] =" 0 "[3] =" 1 "[4] =" release "[5] =" i386-redhat-linux-gnu ") BASH_VERSION = '2. 05b. 0 (1)-release 'Colors =/etc/DIR_COLORS.xterm COLUMNS = 80 DIRSTACK = () DISPLAY =: 0.0... 2. 5. run the unset command to clear the environment variable set. you can set the value of an environment variable. Run the unset command to clear the environment variable values. If no value is specified, the variable value is set to NULL. Example: $ export TEST = "Test... "# Add an environment variable TEST $ env | grep TEST # This command is input to prove that the environment variable TEST already has TEST = Test... $ unset $ TEST # Delete the environment variable TEST $ env | grep TEST # this command has no output, proving that the environment variable TEST already exists 2. 6. if the readonly command is used to set the read-only variable, the variable cannot be modified or cleared. Example: $ export TEST = "Test... "# Add an environment variable TEST $ readonly TEST # set the environment variable TEST as read-only $ unset TEST # you will find that this variable cannot be deleted-bash: unset: TEST: cannot unset: readonly variable $ TEST = "New" # this variable cannot be modified-bash: TEST: readonly variable environment variable settings are located in the/etc/profile File. if you need to add a new environment variable, you can add the subordinate row exportpath = $ path:/path1:/path2:/pahtN. note: these commands are only valid for the current process and child processes. if you want to modify them permanently, you need to modify the relevant configuration files and then source these configuration files to make them take effect. 3. the solution to environment variable problems is back to the two problems mentioned in the overview. when-bash: [commandname]: command notfound appears, use the find command to locate the relevant commandname executable file and add the PATH to the PATH. For the second problem, the solution is similar, but the environment variable to be modified is changed to LD_LIBRARY_PATH. 4. for more information about the configuration file and how to modify PAHT, see blog: bash and PATH http://www.2cto.com/os/201304/203701.html
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.