Basic OPS: Shell Getting Started

Source: Internet
Author: User
Tags variable scope

First, System variables

Perform an env, set view system, or environment variable directly at the command line prompt. ENV displays user environment variables, set shows the shell predefined variables and user variables. Can be exported to user variables through export.

[[email protected] ~]# envxdg_session_id=1hostname=leoselinux_role_requested=term=xtermshell=/bin/ bashhistsize=1000ssh_client=192.168.116.1 61498 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:*.arc=01;31:*.arj=01;31:* . taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*. zip=01;31:*.z=01;31:*. Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31 :*. tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:* . alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=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:*.webm=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:/usr/sbin:/usr/bin:/root/binpwd=/rootlang=en_us. utf-8selinux_level_requested=histcontrol=ignoredupsshlvl=1home=/rootlogname=rootssh_connection=192.168.116.1 61498 192.168.116.129 22lessopen=| | /usr/bin/lesspipe.sh%sxdg_runtime_dir=/run/user/0_=/usr/bin/envoldpwd=/server/web[[email protected]~]#  

Enter the command at the command line and the relevant content will be displayed:

$SHELL default SHELL
$HOME Current User Home directory
$IFS internal Field delimiter
$LANG Default language
$PATH Default Executable program path
$PWD current Directory
$UID Current User ID
$USER Current User
$HISTSIZE history command size, command execution time can be set by Histtimeformat variable
$RANDOM randomly generate an integer from 0 to 32767
$HOSTNAME Host Name

Second, common variables and temporary environment variables

Common variable definition: var=value
Temporary environment variable definition: Export var=value
Variable reference: $VAR
Here's a look at the difference between them:
The environment variable scope of the shell process is the shell process, and when export is imported into a system variable, the scope is the shell process and its shell child process.

[[email protected] ~]# VAR=value[[email protected] ~]# export VAR=value[[email protected] ~]# $VARbash: value: command not found

So the variables defined in the current shell must be export, otherwise they will not be referenced when writing the script.
Like what:

[[email protected] ~]# leo=520[[email protected] ~]# vim env_leo.sh[[email protected] ~]# cat env_leo.sh #!/bin/shecho $leols[[email protected] ~]# bash env_leo.sh anaconda-ks.cfg  env_leo.sh[[email protected] ~]# export leo=520[[email protected] ~]# bash env_leo.sh 520anaconda-ks.cfg  

It is also important to note that all user-defined variables are cleared after exiting the terminal.

Three, Position variable
[[email protected] ~]# bash local.sh  

Iv. Special variables

$ A script's own name
$? Returns whether the previous command succeeded, 0 for execution, and not 0 for execution failure
$# Number of positional parameters
$* all positional parameters are viewed as a string
[email protected] Each positional parameter is considered a separate string
$$ Current Process PID
$! Previous PID for running a background process

Variable references:
Assignment operators
Example
= Variable Assignment
+ = Two variables added

1. Custom variables and references

[[email protected] shell]# VAR=123[[email protected] shell]# echo $VAR123[[email protected] shell]# VAR+=456[[email protected] shell]# echo $VAR123456

All variable references in the shell use the $ character followed by the variable name.
Sometimes an individual special character can affect a normal reference, so you need to use ${var}, for example:

The result is copied as a variable:

The inverse apostrophe here is equivalent to $ (), which is used to execute the shell command.

The difference between a single quote and a double quote:

There is no difference in looking at it alone:

[[email protected] ~]# A=1[[email protected] ~]# test="$A 2 3"[[email protected] ~]# echo $test1 2 3[[email protected] ~]# test=‘$A 2 3‘[[email protected] ~]# echo $test$A 2 3[[email protected] ~]# 单引号是告诉Shell忽略特殊字符,而双引号则解释特殊符号原有的意义,比如$、!。

Basic OPS: Shell Getting Started

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.