[Chapter II] Shell variables

Source: Internet
Author: User
Tags rar xspf

One, variable
    • Variables are the use of a simpler string instead of some special-meaning settings and data. Taking path, this path replaces the absolute path of all common commands, and with the variable path, we no longer need to enter the global path when we run a command, just enter the command name.
[[email protected] ~]# echo $PATH        <== output PATH variable/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/ Root/bin[[email protected] ~]# echo $HOME        <== represents the current user's home directory/root[[email protected] ~]# echo $PWD        <== Show current directory/ Root[[email protected] ~]# echo $LOGNAME        <== indicates the current login root
    1. Command env
[[email protected] ~]# env <== lists all system variables for system presets hostname=shellselinux_role_requested=term=xtermshell=/bin/ bashhistsize=1000ssh_client=192.168.66.246 51689 22selinux_use_current_range=ssh_tty=/dev/pts/0user=rootls_ Colors=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37 ; 41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz= 01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*. Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31 :*. deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*. jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*. xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*. Mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf= 01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01; 35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36 :*. au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:* . wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:mail=/var/spool/mail/rootpath=/usr/local/sbin:/usr /local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binpwd=/rootlang=en_us. utf-8selinux_level_requested=histcontrol=ignoredupsshlvl=1home=/rootlogname=rootssh_connection=192.168.66.246 51689 192.168.31.186 22lessopen=|/usr/bin/lesspipe.sh%sg_broken_filenames=1_=/bin/env
    • The values of these environment variables are different when you log in to different users. is currently the environment variable that displays the root user
  • HOSTNAME: Name of the <== host
  • Shell: <== The current user's shell type
  • Histsize: <== historical record number
  • Mail: <== The current user's message store directory
  • PATH: <== This variable determines to which directories the shell will look for commands or programs
  • PWD: <== Current directory
  • LANG: <== This is a language-dependent environment variable, and a multi-locale variable can modify the environment for this environment variable
  • Home: <== The current user's home directory
  • LOGNAME: <== The current user's login name
  • The env command displays variables that are only environment variables, and the system presets change in fact there are many more. The system preset variables can be displayed by set.
2. Command Set
[[email protected] ~]# set <== The system preset variables are displayed Bash=/bin/bashbashopts=checkwinsize:cmdhist:expand_aliases:ex tquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepathbash_aliases= () bash_argc= () bash_argv= () bash_cmds= () bash_lineno= () bash_source= () bash_versinfo= ([0]= "4" [1]= "1" [2]= "2" [3]= "1" [ 4]= "Release" [5]= "I386-redhat-linux-gnu") bash_version= ' 4.1.2 (1)-release ' colors=/etc/dir_colorscolumns= 80dirstack= () euid=0groups= () g_broken_filenames=1histcontrol=ignoredupshistfile=/root/.bash_historyhistfilesize =1000histsize=1000home=/roothostname=shellhosttype=i386id=0ifs=$ ' \t\n ' Lang=en_us. Utf-8lessopen= ' |/usr/bin/lesspipe.sh%s ' lines=24logname=rootls_colors= ' Rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so =01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34; 42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*. TXZ=01;31:*.ZIP=01;31:*.Z=01;31:*. Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31 :*. deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*. jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*. xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*. mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*. qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv =01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01; 35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01; 36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36: ' MACHTYPE= I386-redhat-linux-gnumail=/var/spool/mail/rootmailcheck=60opterr=1optind=1ostype=linux-gnupath=/usr/local/sbin:/usr/local /bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binpipestatus= ([0]= "0") ppid=1162prompt_command= ' printf ' \033]0;%[email  protected]%s:%s\007 "" ${user} "" ${hostname%%.*} "" ${pwd/# $HOME/~} "' ps1= ' [\[email protected]\h \W]\$ ' PS2 = ' > ' ps4= ' + ' pwd=/rootselinux_level_requested=selinux_role_requested=selinux_use_current_range=shell=/bin/ Bashshellopts=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitorshlvl=1ssh_client= ' 192.168.66.246 51689 ' ssh_connection= ' 192.168.66.246 51689 192.168.31.186 ' ssh_tty=/dev/pts/0term=xtermuid= 0user=root_=setcolors=/etc/dir_colors
    • The SET command can display not only system preset variables, but also user-defined variables
[[email protected] ~]# a=1            <== definition variable a=1[[email protected] ~]# echo $a            <== output variable a1[[email protected] ~]# Set | g Rep ' ^a '            <== see if there are variables in the set aa=1
    • Although you can customize the variable above, the variable can only take effect in the current shell.
[[email protected] ~]# echo $a            <== output variable a1[[email protected] ~]# bash            <== into a sub-shell environment [[Email protected] ~]# echo $a            <== output variable a[[email protected] ~]# exit            <== exit the child shell and return to the original Shellexit[[email protected] ~]# echo $a            <== output Variable A1
    • You can use the Bash command to open a child shell, where the previously set $a variable no longer exists, exit the current shell back to the original shell, $a still, if you want to keep the environment variable effective, both methods
    • 1) directly use the command export to define the variables
[[email protected] ~]# export a=1         <== define the variable [[email protected] ~]# echo $a            <== output variable a1[[email protected] ~]# BA SH            <== into a sub-shell environment [[email protected] ~]# echo $a            <== output variable a1[[email protected] ~]# exit            <== Exit the child shell and return to the original Shellexit
    • 2) Modify/etc/profile to allow all users in the system to log in and use the command
[[email protected] ~]# vim/etc/profile            <== in the last line of the/etc/profile file add export a=1export a=1[[email protected] ~]# Source/etc/profile            <== make/etc/profile effective
    • Rules for defining custom variables
    • The format of the variable is "a=b", where a is the variable name, the contents of the B variable, and no space on either side of the equal sign
    • Variables can only consist of numbers, letters, and underscores, and cannot start with a number
    • When the contents of a variable have special characters (such as spaces), you need to add single quotation marks
[[email protected] ~]# b= ' vforbox xiaoling '            <== define variable for Vforbox xiaoling[[email protected] ~]# echo $b           <== Output $ Bvforbox xiaoling
    • If there is a single quotation mark in the contents of the variable, double quotes will be added.
[Email protected] ~]# c= "1 ' 2" [[email protected] ~]# echo $c 1 ' 2
    • After setting a variable, if you want to cancel a variable, simply enter the unset variable name
[[email protected] ~]# echo $d        <== output $d1[[email protected] ~]# unset d        <== un-defined variables [[email protected] ~]# Echo $d        <== Output $d
  second, the system environment variable and personal environment variable configuration file
    • So where do these variables exist in Linux? Why do users have these variables when they log in to the shell?
    • /etc/profile preset A number of important variables, path,user,logname,mail,inputrc,hostname,histsize,umask, etc.
    • /ECT/BASHRC main presets Umask and PS1. This PS1 is the string of characters that precedes us when we enter a command.
  • [[email protected] ~]# echo $PS 1 <== output $PS 1
  • [\[email protected]\h \w]\$
  • Wherein, \u refers to the user, \h refers to the host name, \w refers to the current directory, \$ refers to the character # (ordinary user is displayed $)
  • There are several hidden files under each user's home directory
  • . bash_profile defines the user's personalization path and the file name of the environment variable, which each user can use to enter their own shell information
  • . BASHRC This file contains bash information for your own shell, which is read when you log in or every time you open a new shell, and you can write your own custom aliases or variables to the file
  • . bash_histry the file always records the command history
  • . bash_logout when exiting the shell, the file is executed and some cleanup work can be put into this file

[Chapter II] Shell variables

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.