SHELL variables for the collation of notes

Source: Internet
Author: User
Tags variable scope


Types of variables:

1. Custom variables

Define variables: Variable name = variable value variable name must start with a letter or underscore, case sensitive url1=test.3glong.com
Reference variable: $ variable name or ${variable name}
View variables: Echo $ variable name set (all variables: including custom variables and environment variables)
Cancel variable: unset variable name
Scope: Valid only in the current shell

Example:

2. Environment variables
Defining environment Variables: Method One export url2=test2.3glong.com
Method two export URL1 convert a custom variable to an environment variable
Reference environment variable: $ variable name or ${variable name}
View environment variables: ECHO $ variable name env For example Env |grep BACK_DIR2
Cancel environment variable: unset variable name
Variable scope: Valid at current shell and child shell
Example:


[[email protected] ~]# url1=test.3glong.com             #定义自定义变量 [[email protected] ~]# export url2=test2.3glong.com  # Defining environment Variables [[email protected] ~]# /bin/bash               #再开启一个bash进入子shell [[email protected] ~]# pstreeinit─┬─abrtd      ├─acpid     ├─anacron     ├─atd      ├─auditd───{auditd}     ├─console-kit-dae───63*[{ console-kit-da}]     ├─crond     ├─cupsd      ├─dbus-daemon     ├─dhclient      ├─hald─┬─hald-runner─┬─hald-addon-acpi     │      │              └─hald-addon-inpu     │      └─{ hald}     ├─login───bash     ├─master─┬─pickup      │        └─qmgr     ├─5*[ mingetty]     ├─rpc.statd     ├─rpcbind      ├─rsyslogd───3*[{rsyslogd}]     ├─sshd───sshd───bash───bash───pstree     #确认处于子shell      ├─udevd───udevd      └─xinetd[[email protected] ~]# echo  $url 1             #输出自定义变量                                                                     #发现什么都没有 [[email protected] ~]# echo  $url 2test2.3glong.com                                    #环境变量输出了 [[email protected] ~]# exit                           # Exits the current sub-shellexit[[email protected] ~]# echo  $url 1test.3glong.com                                        #自定义变量只能当前shell使用 [[ Email protected] ~]# echo  $url 2test2.3glong.com[[email protected] ~]# export url1                 #转换i自定义变量为环境变量 [[email protected]  ~]# /bin/bash[[email protected] ~]# echo  $url 1                 #进入子shelltest .3glong.com                                         # The child shell is normal.


3. Position variables
$ $4 $ $6 $7 $8 $9 ${10}

[[email protected] ~]# vim test.sh echo "2nd position parameter is $" echo "1th positional parameter is" echo "the 4th position parameter is $4" echo "All parameters are: $*" echo " All parameters are: [email protected] The number of "echo" parameters is: $# The PID of the current process is: $$ "Echo ' $1= ' $1echo ' $2= ' $2echo ' $3= ' $3echo ' $*= ' $*echo ' [emai L protected]= ' [email protected]echo ' $#= ' $ #echo ' $$= ' $$


4. Pre-defined variables
The name of the foot
$* all the parameters
[Email protected] all the parameters
Number of $# parameters
$$ PID of the current process
$! PID of the previous background process
$? The return value of the previous command 0 indicates success
Considerations when defining or referencing variables:
"" Weak reference
' Strong references '

[Email protected] ~]# School=emlog[[email protected] ~]# echo "$school blog" emlog blog[[email protected] ~]# echo ' $schoo L Blog ' $school blog


This article is from the "Dragon Blog" blog, please be sure to keep this source http://3glong.blog.51cto.com/11271871/1782248

SHELL variables for the collation of notes

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.