Bash string processing (compared with Java)-3. assign values to the (string) Variable

Source: Internet
Author: User

Bash string processing (compared with Java)-3. assign values to the (string) Variable

In Java
Simple assignment
S = "Hello ";
 
Assignment of multiple variables at a time
S1 = s2 = "Same ";
 
Assign a value to an integer (convert an integer to a string)
Int I = 1234;
S = String. valueOf (I );
Or s = "" + I;
 
In Bash
Value assignment (=)
The value assignment operation is in the form of VAR = STRING
Remember: there cannot be spaces on both sides of the equal sign!
Advanced Bash-Scripting Guide: 4.2. Variable Assignment writes
=
The assignment operator (no space before and after)

Caution
Do not confuse this with = and-eq, which test, rather than assign!
Note that = can be either an assignment or a test operator, depending on context.
 
Assign a value to a String constant
Does not contain any special characters
Example: S1 = Literal
Example: S1 = 'literal'
Example: S1 = "Literal"
Example: S1 = $ 'literal'
 
Case with spaces
Example: S2 = Hello \ World
Example: S2 = 'Hello world'
Example: S2 = "Hello World"
Example: S2 = $ 'Hello world'
 
Case with single quotes
Example: S3 = Yes, \ I \'m \ Basher
Example: you cannot use single quotes.
Example: S3 = "Yes, I'm Basher"
Example: S3 = $ 'Yes, I \'m Basher'
 
Double quotation marks
Example: S4 = It \ is \ very \ "Stupid \".
Example: S4 = 'it is very "Stupid ".'
Example: S4 = "It is very \" Stupid \"."
Example: S4 = $ 'it is very "Stupid ".'
 
Multi-Line Text
Example: S5 = 'Hello
World'
Example: S5 = "Hello
World"
Example: S5 = $ 'Hello
World'
 
Value assigned to another variable
Common variables
Example: S2 = $ S1
Example: S2 =$ {S1}
 
Location parameters
Example: S3 = $1
Example: S3 =$ {1}
 
Assign a value to the command line to execute the output (replace the command)
Format: S2 = 'commandline'
Format: S2 = $ (commandline)
Format: S2 = "'commandline '"
Format: S2 = "$ (commandline )"
 
Create a script and output the Sha-Bang and Bash execution file paths, that is, the first line of the script.
[Root @ jfht ctmw] # echo '#! '$ (Which bash)> script. sh
[Root @ jfht ctmw] # cat script. sh
#! /Bin/bash
[Root @ jfht ctmw] #
 
Assign a value to a more complex string replacement result
Example: S1 = "Your account is $ {ACCOUNT }"
Example: S2 = "Current Working Directory is $ (pwd )"
 
Null String
Example: S =
 
Assign the same value to multiple variables at a time.
In Java, we can write a = B = "String ";
However, in Bash, we can only write A = $ B = "STRING"
 
Example: A = $ B = $ C
Assign variable C to B first, and then assign variable B to.
Note that it cannot be written as A = B = C, and the result of A = B = C is completely different.
 
Assign a value to the file content
Format: CONTENT = $ (cat filename)
Note: If the file specified by filename is a binary data file, the actual CONTENT may be inconsistent.
 
Text Files
[Root @ jfht ~] # Cat file.txt
1234
2345
4567
5678
[Root @ jfht ~] # Ls-l file.txt
-Rw-r -- 1 root 20 09-07 07:48 file.txt
[Root @ jfht ~] # S = $ (cat file.txt)
[Root @ jfht ~] # Echo $ S
1234 2345 4567 5678
[Root @ jfht ~] # Echo "$ S"
1234
2345
4567
5678
[Root @ jfht ~] # Echo $ {# S}
19
[Root @ jfht ~] #
The above $ {# S} is used to obtain the string length.
The length of a text file is inconsistent, but the string content is consistent.
 
Binary files
[Root @ smsgw root] # ls-l/usr/bin/ac
-Rwxr-xr-x 1 root 18452/usr/bin/ac
[Root @ smsgw root] # S = $ (cat/usr/bin/ac)
[Root @ smsgw root] # echo $ {# S}
13516
[Root @ smsgw root] # S = "$ (cat/usr/bin/ac )"
[Root @ smsgw root] # echo $ {# S}
13511
[Root @ smsgw root] #
The above $ {# S} is used to obtain the string length.
 
Q: Why is the content of the file loaded with cat different from that of the actual file?
 
Assign an integer
Since the variables in Shell are non-typed, all values can be directly assigned to the variables without any conversion means.
Format: STR = SOME_INTEGER
However, if a formula is assigned to a variable, it is not automatically calculated. You can use the following methods:
Format 1: STR = $ [EXPR]
Format 2: (STR = EXPR ))
Format 3: let STR = EXPR
Format 4: declare-I STR = EXPR
Format 5: STR = 'expr expr'
Format 6: STR = $ (expr EXPR)
 
[Root @ jfht ~] # STR = 1234
[Root @ jfht ~] # Echo $ STR
1234
[Root @ jfht ~] # STR = 1234 + 4321
[Root @ jfht ~] # Echo $ STR
1234 + 4321
Directly assigning the formula to the variable or the formula itself is not the calculation result.
[Root @ jfht ~] # STR = $[1234 + 4321]
[Root @ jfht ~] # Echo $ STR
5555
[Root @ jfht ~] # (STR = 1234 + 4321 ))
[Root @ jfht ~] # Echo $ STR
5555
[Root @ jfht ~] # Let STR = 1234 + 4321
[Root @ jfht ~] # Echo $ STR
5555
[Root @ jfht ~] # Declare-I STR = 1234 + 4321
[Root @ jfht ~] # Echo $ STR
5555
[Root @ jfht ~] # STR = 'expr 1234 + 4321'
[Root @ jfht ~] # Echo $ STR
5555
[Root @ jfht ~] # STR = $ (expr 1234 + 4321)
[Root @ jfht ~] # Echo $ STR
5555
[Root @ jfht ~] #

 

Author: "Bash @ Linux"
 

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.