Ways to set up and view environment variables in Linux

Source: Internet
Author: User
Tags echo command

1. Display environment Variables home$ echo $HOME/home/Redbooks2. Set a new environment variable hello$ export HELLO="hello!"$ echo $HELLOHello!3. Use the env command to display all environment variables $ envhostname=Redbooks.safe.orgPVM_RSH=/usr/bin/Rshshell=/bin/Bashterm=xtermhistsize= +...4. Use the SET command to display all locally defined shell variables $SetBASH=/bin/Bashbash_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.xtermcolumns= theDirstack=() DISPLAY=:0.0...5. Use the unset command to clear the environment variable set to set the value of an environment variable. Clear the value of the environment variable with the unset command. If no value is specified, the value of the variable is set to NULL. Examples are as follows: $ export TEST="Test ..."#增加一个环境变量TEST $ env|grep test #此命令有输入 to prove that the environment variable test already exists test=Test ... $ unset $TEST #删除环境变量TEST $ env|grep test #此命令没有输出 to prove that the environment variable test already exists.6. Use the readonly command to set a read-only variable if the readonly command is used, the variable cannot be modified or erased. Examples are as follows: $ export TEST="Test ..."#增加一个环境变量TEST $ReadOnlyTest #将环境变量TEST设为只读 $ unset test #会发现此变量不能被删除-bash:unset:test:cannot unset:ReadOnlyvariable$ TEST="New"#会发现此也变量不能被修改-bash:test:ReadOnlythe settings for the variable environment variable are located in/etc/profile file If you need to add a new environment variable can be added subordinate row export path= $path:/path1:/path2:/pahtn .7. Accessing and setting environment variables with C programs for users of C programs, you can use the following three functions to set or access an environment variable. Getenv () accesses an environment variable. The input parameter is the name of the variable that needs to be accessed, and the return value is a string. If the environment variable being accessed does not exist, NULL is returned. Setenv () a function that sets an environment variable inside a program. Unsetenv () a function that clears a particular environment variable. In addition, there is a pointer variable environ, which points to a list that contains all the environment variables. The following program can print out all environment variables in the current operating environment: #i ncludeextern Char**environ;intMain () {Char**var;  for(var=environ;*var!=null;++var) printf ("%s \ n",*var); return 0; } environment variable settings under Linux #/etc/profile# System wide environment and startup programs, forlogin setup# Functions and aliases goinch/etc/Bashrcpathmunge () {if! echo $PATH | /bin/egrep-q"(^|:) $ ($|:)"; Thenif[" $"=" After" ] ; Thenpath= $PATH: $1ElsePATH=$1: $PATHfifi}# Path manipulationif[' id-u ' =0 ]; Thenpathmunge/Sbinpathmunge/usr/Sbinpathmunge/usr/local/Sbinfipathmunge/usr/x11r6/bin Afterunset pathmunge# No core Files bydefaultUlimit-s-c0>/dev/NULL 2>&1USER="' Id-un '"LOGNAME=$USERMAIL="/var/spool/mail/$USER"HOSTNAME= '/bin/hostname ' Histsize= +if[-Z"$INPUTRC"-A! -F"$HOME/.inputrc" ]; THENINPUTRC=/etc/inputrcfiexport PATH USER LOGNAME MAIL HOSTNAME histsize INPUTRC forIinch/etc/profile.d/*. Sh; Doif [-R "$i"]; Then. $ifidoneunset ijava_home= "/opt/j2sdk1.4.2_01" mysql_home= "/var/mysql" classpath=.: $JAVA _home/lib/tools.jar: $JAVA _ Home/lib/dt.jarpath= $PATH: $JAVA _home/bin: $MYSQL _home/bin:/opt/apache-ant-1.5.4/binexport java_home CLASSPATH Pathcvsroot=/cvsroot;export cvsrootlinux System environment variable/etc/profile is effective for the whole system. Make sure you're using bash modify ~/.bash_profile to write the environment variables you want to set in such as Java_home=/usr/local/jdkexport Java_ HOME--------------------------------------------------------------------------------------------------------------------- The variable types of--1.linux are divided by the lifetime of the variables, and the Linux variables can be divided into two categories: 1.1 Permanent: The configuration file needs to be modified, and the variable will be permanently applied. 1.2 Temporary: With the Export command declaration, the variable is invalidated when the shell is closed. 2. Three ways to set a variable 2.1 add a variable in the/etc/profile file "for all Users" (permanent) Use VI to add a variable to the file/etc/profile file, which will be valid for all users under Linux and is "permanent". For example: Edit the/etc/profile file, add CLASSPATH variable # vi/etc/profileexport classpath=./java_home/lib; $JAVA _home/jre/ LIB Note: If you want to run the file immediately after modifying it, you can only take effect the next time you re-enter the user. Source/etc/profile. 2.2 Add variable "to single user (permanent)" in the. bash_profile file in the user directory use VI to add variables to the. bash_profile file in the user directory, and change the amount to be valid only for the current user and is "permanent". Example: Edit the Guok user directory (/home/guok). bash_profile$ Vi/home/guok/.bash.profile Add the following: Export classpath=./java_home/lib; $JAVA _home/jre/ LIB Note: If you want to take effect immediately after modifying a file, run $ source/home/guok/.bash_profile or you can only take effect the next time you re-enter the user. 2.3 Directly run the Export command to define the variable "valid only for the current shell (temporary)" in the Shell's command line directly using the [export variable name = variable value] To define the variable, the variable only in the current shell (bash) or its child shell (bash ) is valid, the shell is closed, the variable is invalidated, and when the new shell is opened, there is no such variable, and it needs to be redefined. 3. View of environment variables 3.1 use the echo command to view a single environment variable. For example: Echo $PATH 3.2 Use env to view all environment variables. For example: env3.3 uses set to view all locally defined environment variables. Unset can delete the specified environment variable. 4. Common environment variables path determines which directories the shell will look for commands or programs home current user home directory histsize history number logname Current user's login hostname refers to the name of the host shell  The current user shell type Languge language-related environment variables, multiple languages can modify this environment variable mail current user's message storage directory PS1 basic prompt, for the root user is #, for the average user is $

Ways to set up and view environment variables in Linux

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.