Shell variables and operators

Source: Internet
Author: User

-Local variable-commands that affect Variables
-Environment variables-quotation marks
-Replace variables-Operators
-Location variable-expression replacement
-Standard shell variable-operator priority
-Special Variables

-Variable:
-What is a shell variable?
-Local Variables
-Environment Variables
-Replace variables (display variables)
-Location variable
-Standard Variables
-Special Variables
-Commands that affect Variables

-Local variables:
-Local variables are used in users' Current shell lifecycle scripts.
-Variable-name = value
-Set: displays all local variables.
-Readonly variable-name
# LOCALTEST = "test"
# Echo $ {LOCALTEST}
Test
# Echo $ LOCALTEST
Test
# Set view variables through set
# Exit and check whether the LOCALTEST variable exists.
# Readonly LOCALTEST
# LOCALTEST = "test"
# Readonly view the read-only variables in the Current shell
# Readonly-p
-Environment Variables
-Environment variables are used for all user processes (often referred to as subprocesses ). the logon process is called the parent process. user processes executed in shell are called sub-processes. unlike local variables (only for the current shell), environment variables can be used for all sub-processes, including editors, scripts, and applications.
-$ HOME/. bash_profile (/etc/profile)
-Export
-Env
# Cat/etc/profile
# Export Antiy = "Xi'an"
# Env and search for the variable Antiy
# Export can also be viewed
# Readonly Antiy is set to read-only
# Export Antiy = "xidian" to check whether it can be modified. The system prompts "no" because it is read-only.

-Variable replacement:
-Replace its name with the value of the variable.
-Echo
-Add $ before the variable name and use the echo command to display the values of a single variable.
# Testvar = "this is a test"
# Echo $ testvar
This is a test
# Echo $ {testvar}
Variable replacement-continued
$ {Variable name} shows the actual value to variable name
$ {Variable name: + value} If variable name is set, its value is displayed. Otherwise, it is null!
$ {Variable name :? Value} If variable name is not set, the user-defined error message value is displayed!
$ {Variable name:-value} if no variable name is set, its value is displayed.
$ {Variable name: = value} if no variable name is set, set its value and display
# Echo $ {testvar: + "xidian "}
Xidian
# Echo $ {testvar1: + "xidian "}
Null
# Echo ${testvar1 :? "No defined "}
Bash: testvar1: no defined
# Echo $ {testvar1:-"xidian "}
# Echo $ {testvar:-"xidian "}
Xidian
# Echo $ {testvar: = "xidian "}
This is a test
# Echo $ {testvar2: = "xidain "}
Xidian
# Echo $ testvar2
Xidian

-Clear variables:

# Echo $ testvar
This is a test
# Unset testvar
# Echo $ testvar
Null
# Echo $ testvar2
# Readonly testvar2
# Unset testvar2
-Location variable:
-The position variable indicates $0, $1, $2,... $9
-Use location parameters to scripts
-Pass parameters to system commands
Example01:
#! /Bin/bash
# Parm01
Echo "this is the Script Name: $0"
Echo "This is the script's 1st location parameter: $1"
Echo "This is the script's 1st location parameter: $2"
Echo "This is the script's 1st location parameter: $3"
Echo "This is the script's 1st location parameter: $4"
Echo "This is the script's 1st location parameter: $5"
Echo "This is the script's 1st location parameter: $6"
Echo "This is the script's 1st location parameter: $7"
Echo "This is the script's 1st location parameter: $8"
Echo "This is the script's 1st location parameter: $9"
Example02:
#! /Bin/bash
# Parm02
Find/home/shell/-name $1-print

-Standard variables:
-Bash creates some standard environment variables by default, which can be defined in/etc/profile.
-EXINIT
-HOME
-IFS
Echo $ IFS
IFS = ':'
-LOGNAME
-MAIL
-MAILCHECK
-MAILPATH
-TERM
-PATH
-TZ
-PS1
# Set | grep "PS"
PS1 = '[\ u @ \ h \ w] \ $'
PS2 = '>'
-PS2
-PWD
-SHELL
-MANPATH
-TERMINFO
-Special variables:
-$ #: Number of parameters passed to the script
-$ *: Displays all parameters passed to the script in a single string. Different from the location variable, this option can contain more than 9 parameters.
-$: ID of the current process running the script
-$! : ID of the last process running in the background
-$ @: It is the same as $ *, but it is enclosed by quotation marks and each parameter is returned in quotation marks.
-$-: Displays the current options used by the shell, which have the same functions as the set command.
-$? : Displays the exit status of the last command. 0 indicates no error. Other values indicate errors.
#! /Bin/bash
# Parm01
Echo "this is the Script Name: $0"
Echo "This is the script's 1st location parameter: $1"
Echo "This is the script's 1st location parameter: $2"
Echo "This is the script's 1st location parameter: $3"
Echo "This is the script's 1st location parameter: $4"
Echo "This is the script's 1st location parameter: $5"
Echo "This is the script's 1st location parameter: $6"
Echo "This is the script's 1st location parameter: $7"
Echo "This is the script's 1st location parameter: $8"
Echo "This is the script's 1st location parameter: $9"
Echo "Number of displayed parameters: $ #"
Echo "show all Script Parameters: $ *"
Echo "display process ID: $"
Echo "shows the status after the previous command is run: $? "
Commands that affect variables:
-Declare
-Export
-Readonly
-Set
-Shift
-Typeset
-Unset
Quotation marks, double quotation marks, single quotation marks, reverse quotation marks, and backslash
Operator and operator type

  1. Linux cluster Basics
  2. Opera will launch Mobile Linux
  3. Go to linux 2-driver development technology

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.