Shell-2.shell Defining variables

Source: Internet
Author: User
Tags file handling

Shell variables


${#var} to calculate the length of the variable value:

1. Definition of variables

Variable name = "Content assigned to Variable" (string)
Variable name = ' assignment to the contents of a variable ' (string)
Variable name = numeric value (numeric value)

Note: There can be no spaces between the variable name and the equals sign

2. Naming conventions for variable names
• The first character must be a letter (a-z,a-z).
• Punctuation cannot be used.
• The middle cannot have spaces, you can use the underscore (_), preferably not with special characters, poor readability.
• Do not use the keywords in bash

3. "Example"
Name= "Yuanji"
Age=20

4. Using variables
$ variable Name
${variable name} #推荐使用这种形式

Instance
[email protected] yuanji_shell]# cat second.sh
#!/bin/sh
Name= "Yuanji"
Age=20
Src= ' Shell '

Echo $name
Echo $age
Echo ${name}

echo "My name is $name, the age is $age. This is $srcProgram "
echo "My name is $name, the age is $age. This is ${src}program "

Show results
[Email protected] yuanji_shell]# sh second.sh
Yuanji
20
Yuanji
My name is Yuanji,age is 20. This is "not shown properly here"
My name is Yuanji,age is 20. This is Shellprogram

5. Execute the command assignment to the variable (anti-quote ": Output the command execution result to the variable)
Instance
The entire file is read into a variable and then printed directly, and it's easy to understand,
A= ' cat test.txt '; Echo $a

6. Redefine variables (defined variables can be re-assigned value)
[Email protected] home]# name= "Yuanji"
[Email protected] home]# echo $name
Yuanji
[Email protected] home]# name= "Tom"
[Email protected] home]# echo $name
Tom

7. Assign a value to another variable
[Email protected] ~]# name= "Yuanji"
[Email protected] ~]# new_name= $name
[Email protected] ~]# echo $new _name
Yuanji


The following sections are described in follow-up content
For example, using variables in sed and awk,
Upload MySQL data to a variable
Upload the contents of the file to a variable (corresponding to the problem of file handling, the individual rarely uses the shell, basically Perl or Python.) Because this is not the shell's strong place)

The above is a personal point of summary and understanding, the level is not high, the level of writing is very bad, please greatly forgive me.
Can exchange learning together.

My qq:610851588.
Can join my build group (now very few people, hope slowly more up)
Linux Clusters: 183932302
Python, Shell AC Group: 469094734


This article is from the "Go to the Origin dimension" blog, please make sure to keep this source http://yuanji6699.blog.51cto.com/11568362/1772945

Shell-2.shell Defining variables

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.