Variable reference in Shell (two methods: $ variable name, $ {variable name }).

Source: Internet
Author: User

I. Basics

We define a variable (there cannot be spaces on both sides of the equal sign)

Filepath =/var/home/SSS
Filefilename = test001

When referencing this variable, we can directly use the variable name following $.

For example, $ filepath
You can also: $ {filepath}

The advantage of using $ {} Is that the connection between the two variables can be easily realized, and it is clear at the same time.

$ {Filepath}/$ {filefilename}

This will indicate the full path of the file.

---------------------------------------------

2. Sometimes we see

$1, $2, which indicates the parameters passed in during shell execution.

-------------------------------------------

3. We will also see the following forms

1. If the variable meter has a value or is empty, use the value of value.

$ {Variable:-value}


2. If the variable meter has a value or is empty, use the value of value and assign the variable value to value.

$ {Variable: = value}


3. Check whether the variable is empty.

$ {Variable :?}

------------------------------------------

Which of the following are not commonly used?

--------------------------------------------

$0 shell command itself (including the full path)
$1 to $9 indicates the shell Parameters
$ # Number of parameters passed to the script
$ * Display all parameters passed to the script with a single string
$ ID of the script run
$! ID of the last process running in the background
$ @ Is the same as $.
$-Display the current options used by shell.
$? Displays the execution status of the last command. 0 indicates no error.

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.