Summary of variable usage in Shell

Source: Internet
Author: User

Summary of variable usage in Shell

This article mainly introduces the usage of variables in Shell. This article summarizes the syntax and common usage of variables, and provides sample code. For more information, see

Variable definition. It can only start with letters and underscores (_). It is case-sensitive and can contain numbers, letters, and underscores (_). For details, see the official manual.

The Code is as follows:

[Root @ svn shell_example] # yourname = 'linux'

[Root @ svn shell_example] # echo $ yourname

Linux

[Root @ svn shell_example] # YourName = "linux"

[Root @ svn shell_example] # echo "The variable is $ YourName"

The variable is linux

Obtain the script of the current date, which is often used in log cutting. To distinguish daily log files

The following code is generated on January 1, April 27, 2015.

The Code is as follows:

[Root @ svn shell_example] # echo 'date + % Y % m % d'

20150427

[Root @ svn shell_example] # cat today. sh

#! /Bin/bash

TODAY = 'date + %'

YEAR = 'date + % Y % m month % dday'

Echo "TODAY is $ YEAR, $ TODAY"

[Root @ svn shell_example] # sh today. sh

Today is Monday, January 1, April 27, 2015.

Obtain user input from the keyboard. After the following script is executed, the script waits for user input directly until user input is complete and press Enter. The script obtains user input and prints the result.

The Code is as follows:

[Root @ svn shell_example] # cat var. sh

#! /Bin/bash

Read myvar

Echo "myvar is $ myvar

The execution result is as follows:

[Root @ svn shell_example] # sh var. sh

Linux

Myvar is Linux

[/Code]

Get user input from keyboard with prompt information

The Code is as follows:

#! /Bin/bash

# Obtain the user information from the keyboard and print it out.

Echo-n 'enter Your Name: '#-n indicates no line feed.

Read name

Echo "Hi $ name"

Sh name. sh

Enter Your Name: linux

Hi linux

Upgrade script, which successfully accepts the user's two parameter surnames and names, and prints the input on the screen

The Code is as follows:

The Code is as follows:

[Root @ svn shell_example] # cat firstname. sh

#! /Bin/bash

Pw = "123"

Echo

Echo "+ ========= User Logon =========+"

Echo

Echo-n "Enter your user name :"

Read fname lname

Echo-n "enter your password :"

Read passwd

Echo "Dear member $ fname $ lname, hello! You have logged on successfully"

[Root @ svn shell_example] # sh firstname. sh

+ ========= User Logon ===========+

Enter your Username: nasli

Enter your password 123

Dear member, thank you! You have logged on

Note <>: For more exciting tutorials, please pay attention to the help house programming

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.