Summary of variables used in the shell

Source: Internet
Author: User
Tags svn

This article mainly introduces the variables in the shell use summary, this article summed up the variable syntax, common use forms and so on, and give the code examples, the need for friends can refer to the

The definition of a variable can only begin with a letter and an underscore, case-sensitive, and may contain a numeric letter underline. See Official Handbook

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

Gets the script for the current date, commonly used in log cutting, in order to distinguish daily log files

Like the following generation 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 +%a '

Year= ' date +%y year%m month%d days '

echo "Today is $YEAR, $TODAY"

[ROOT@SVN shell_example]# SH today.sh

Today is April 27, 2015, Monday

From the keyboard to obtain user input, the following script executes the script directly wait for the transfer input, until the user input completes and returns, the script obtains the user's 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

Implementation results are 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

#从键盘获取用户信息, and print it out.

Echo-n ' Enter Your Name: ' #-n option indicates no line wrapping.

Read name

echo "Hi $name"

SH name.sh

Enter Your Name:linux

Hi Linux

Upgrade script, which successfully accepts the user's 2-parameter surname and name, and prints the input on the screen

The code reads as follows

The code is as follows:

[Root@svn shell_example]# Cat firstname.sh

#!/bin/bash

pw= "123"

Echo

echo "+======== User Login =========+"

Echo

Echo-n "Please enter your username:"

Read FName lname

Echo-n "Please enter your password:"

Read passwd

echo "Dear member $fname $lname, Hello!" You are logged in successfully "

[ROOT@SVN shell_example]# SH firstname.sh

+======== User Login =========+

Please enter your username: Nass Lee

Please enter your password: 123

Dear member Nass Li, Hello! You have logged in successfully

Note < > : More Wonderful tutorials please focus on Triple programming

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.