Bash string processing (against Java)-2. String Representation (String constant)

Source: Internet
Author: User

Bash string processing (against Java)-2. String Representation (String constant)

In Java
You know!
Use single quotes to indicate character constants: 'C'
Double quotation marks are used to indicate a String constant: "hello world"
 
Java character escape
\ U ???? Four-digit decimal Unicode Character
\??? Three octal characters
\ N newline (\ u000a) (Insert a newline in the text at this point .)
\ T horizontal tab (\ u0009) (Insert a tab in the text at this point .)
\ B backspace (\ u0008) (Insert a backspace in the text at this point .)
\ R press enter (\ u000d) (Insert a carriage return in the text at this point .)
\ F form feed (\ u000c) (Insert a formfeed in the text at this point .)
\ 'Single quotes (\ u0027) (Insert a single quote character in the text at this point .)
\ "Double quotation marks (\ u0022) (Insert a double quote character in the text at this point .)
\ Backslash (\ u005c) (Insert a backslash character in the text at this point .)
 
Note: A lot of information may not be correct on the Internet. It is better to check Characters on the Oracle Website:
Http://download.oracle.com/javase/tutorial/java/data/characters.html
 
In Bash
String usage
Nowhere is a string in Bash. It can
(1) As a command;
(2) As a command line parameter;
(3) as the variable value;
(4) as the name of the redirected file;
(5) As the input string (here string, here document );
(6) As command line output.
 
String Representation
In Bash, string constants can be expressed in multiple ways.
(1) do not use any quotation marks, such as hello
(2) Use a pair of single quotes, such as 'Hello'
(3) Use a pair of double quotation marks, such as "hello"
(4) enclose the dollar sign with a pair of single quotes, for example, $ 'Hello'
If the string does not contain special characters, the preceding representation is equivalent.
 
Friendly tip: the representation of the string can be combined at any time. For example, h'e' "l" $ 'lo' is equivalent to hello.
 
[Root @ jfht ~] # Echo hello
Hello
[Root @ jfht ~] # Echo 'hello'
Hello
[Root @ jfht ~] # Echo "hello"
Hello
[Root @ jfht ~] # Echo $ 'hello'
Hello
[Root @ jfht ~] # Echo h'e' "l" $ 'lo'
Hello
[Root @ jfht ~] #
 
Question about String Representation
Before you learn more about the string representation, answer the following questions:
Question 1: How should a string contain spaces?
Question 2: How should a string contain tabs?
Question 3: How should a string contain single quotes?
Question 4: How should I represent a string containing double quotation marks?
Question 5: How should I represent a string containing $?
Question 6: The string contains #. How should I represent it?
Question 7: How can a string contain line breaks and multiple lines?
Question 8: How should a string contain a backslash?
Question 9: How should I express an exclamation mark in a string?
 
No quotation marks (one of quoting mechanisms: the escape character)
When no quotation marks are used, all special characters have special functions. To disable them, escape them with escape characters.
Man bash wrote:
A non-quoted backslash (\) is the escape character. It preserves
Literal value of the next character that follows, with the exception
<Newline>. If a \ <newline> pair appears, and the backslash is not
Itself quoted, the \ <newline> is treated as a line continuation (that
Is, it is removed from the input stream and specified tively ignored ).
 
Spaces are used to separate commands and parameters, parameters and parameters, value assignment of variables, and commands. Therefore, if a string contains spaces, escape spaces.
[Root @ jfht ~] # STR = hello world
-Bash: world: command not found
[Root @ jfht ~] # STR = hello \ world
 
A line break is a separator between command lines (the other is a semicolon). If the string is long, you can escape the line break to continue the line break.
[Root @ jfht ~] # STR = hello \
> World
[Root @ jfht ~] #
 
When no quotation marks are enclosed, the character followed by \ will lose its special meaning. For example, pipe line (| ).
[Root @ jfht ~] # STR = hello | world
-Bash: world: command not found
[Root @ jfht ~] # STR = hello \ | world
[Root @ jfht ~] #
 
Single quotes (full quoting)
Advanced Bash-Scripting Guide: Chapter 3. Special Characters wrote
Full quoting [single quote]. 'string' preserves all special characters within STRING. This is a stronger form of quoting than "STRING ".
 
All special characters in single quotes lose their special meanings. In this way, the single quotation mark character itself cannot be expressed in single quotes.
Man bash QUOTING writes:
Enclosing characters in single quotes preserves the literal value
Each character within the quotes. A single quote may not occur
Single quotes, even when preceded by a backslash.
 
 
[Root @ jfht ~] # Echo '$'
$
[Root @ jfht ~] # Echo '''
`
[Root @ jfht ~] # Echo '\'
\
[Root @ jfht ~] # Echo '! '
!
[Root @ jfht ~] # Echo 'in in of string
> Some strings
> End of string'
Begin of string
Some strings
End of string
[Root @ jfht ~] #
 
The following shows the performance of the single quotation mark string in the script.
 
Bash Script: test_single_quote.sh
Bash code
#! /Bin/sh
 
Echo '$'
 
Echo '''
 
Echo '\'
 
Echo '! '
 
Echo 'start of string
Some strings
End of string'
 
[Root @ jfht ~] # Chmod + x./test_single_quote.sh
[Root @ jfht ~] #./Test_single_quote.sh
$
`
\
!
Start of string
Some strings
End of string
[Root @ jfht ~] #
 
Double quotation marks (double quote, partial quoting)
Advanced Bash-Scripting Guide: Chapter 3. Special Characters wrote
Partial quoting [double quote]. "STRING" preserves (from interpretation) most of the special characters within STRING.
In double quotation marks, most special characters lose their special meanings, such as comment (#), command separator (;), pipe line (|), and wildcard (? .
 
[Root @ jfht work191] # echo *
Cgen ct08 hycu2 hyfc2 jlib keyc mdbc msgc sms web_spider xqt_admin xqt_demo xqt_server zjlt_mhr_files zjlt_mhr_server zjlt_mhr_user zjlt_mhr_web
[Root @ jfht work191] # echo "*"
*
[Root @ jfht work191] # echo #

[Root @ jfht work191] # echo "#"
#
[Root @ jfht work191] #
 
Advanced Bash-Scripting Guide: Chapter 5. Quoting writes
This prevents reinterpretation of all special characters within the quoted string -- Comment T $, '(backquote), and \ (escape ). keeping $ as a special character within double quotes permits referencing a quoted variable ("$ variable"), that is, replacing the variable with its value
Man bash wrote:
Enclosing characters in double quotes preserves the literal value
All characters within the quotes, with the exception of $, ', and \.
 
In double quotation marks, special characters include: Dollar sign ($), reverse quotation mark ('), escape character (\), exclamation point (!), You don't need to worry about other special characters. (Why do I write "Exclamation point (!)" here (!)" Please refer to the following description)
 
Man bash wrote:
The characters $ and 'retain their special meaning within double
Quotes.
 
In double quotes, the dollar sign ($) can reference a variable and replace it with the value of the variable, for example
"$ VAR" is replaced with the value of the variable VAR, but "$ VAR123" is replaced with the value of the variable VAR123, so it is best to enclose the variable in braces, avoid unexpected errors;
"$ {VAR}" is replaced with the value of the variable VAR. "$ {VAR} 123" is replaced with the value of the variable VAR, which is later than 123;
"$ {VAR:-DEFAULT}" When VAR is not defined or empty, replace it with DEFAULT; otherwise, replace it with the VAR value;
"$ (Command line)" is equivalent to "'COMMAND line'" and will be replaced with the standard output information for command line execution.
 
The variable VAR is not defined.
[Root @ jfht ~] # Echo "$ VAR"

[Root @ jfht ~] # Echo "$ {VAR }"
 
[Root @ jfht ~] # Echo "$ VAR123"

[Root @ jfht ~] # Echo "$ {VAR} 123"
123
[Root @ jfht ~] # Echo "$ {VAR:-DEFAULT }"
DEFAULT
Now define the VAR variable.
[Root @ jfht ~] # VAR = "Hello World"
[Root @ jfht ~] # Echo "$ VAR"
Hello World
[Root @ jfht ~] # Echo "$ {VAR }"
Hello World
[Root @ jfht ~] # Echo "$ VAR123"

[Root @ jfht ~] # Echo "$ {VAR} 123"
Hello World123
[Root @ jfht ~] # Echo "$ {VAR:-DEFAULT }"
Hello World
Now let's test the command execution output result.
[Root @ jfht ~] # Echo "$ (pwd )"
/Root
[Root @ jfht ~] #
 
In double quotation marks, the reverse quotation marks (') will be replaced with the standard output information for command line execution.
"'LS'" is equivalent to "$ (ls)" and will be replaced with the execution result of the ls command, that is, to list the file names of the current directory;
"'Date + % F'" is equivalent to "$ (date + % F)" and will be replaced with the execution result of date + % F, that is, the date of the day, for example
 
[Root @ jfht ~] # Echo "'pwd '"
/Root
[Root @ jfht ~] # Echo "'date + % F '"
2011-09
[Root @ jfht ~] # Echo "$ (date + % F )"
2011-09-02
[Root @ jfht ~] #
 
Man bash wrote:
The backslash retains its special meaning only when followed
By one of the following characters: $, ', ", \, or <newline>. A double
Quote may be quoted within double quotes by preceding it with a back-
Slash.
 
In double quotation marks, the escape character (\) is used to facilitate the creation of strings containing special characters. The escape character must be preceded \.
\ "Double quotation marks gives the quote its literal meaning
\ $ Dollar sign gives the dollar sign its literal meaning (variable name following \ $ will not be referenced)
\ Backslash, escape character itself gives the backslash its literal meaning
\ 'Reverse quotation marks
\ <Newline> is \ to keep up with the line feed, so the line feed is no longer used, only to continue the line.
 
[Root @ jfht ~] # Echo ""

[Root @ jfht ~] # Echo "hello
> World"
Hello
World
[Root @ jfht ~] #
[Root @ jfht ~] # Echo "hello world \""
Hello world"
[Root @ jfht ~] # Echo "hello world \ $"
Hello world $
[Root @ jfht ~] # Echo "hello world \\"
Hello world \
[Root @ jfht ~] # Echo "hello world \'"
Hello world'
[Root @ jfht ~] # Echo "hello world \
> Yes"
Hello worldyes
[Root @ jfht ~] #
 
In double quotation marks, the exclamation point (!) In the command line environment, it is interpreted as a historical command, but it does not have a special meaning in the script.
Advanced Bash-Scripting Guide: 5.1. Quoting Variables wrote
Encapsulating "! "Within double quotes gives an error when used from the command line. this is interpreted as a history command. within a script, though, this problem does not occur, since the Bash history mechanic is disabled then.
 
In the command line environment, the exclamation point (!) It is called the history expansion character )".
[Root @ jfht ~] # Pwd
/Root
[Root @ jfht ~] # Echo "! "
-Bash :! : Event not found
[Root @ jfht ~] # Echo "! Pwd"
Echo "pwd"
Pwd
[Root @ jfht ~] #
 
When an exclamation point is used in the script, no historical extension is performed.
 
Bash Script: test_exclamation_mark.sh
Bash code
#! /Bin/sh
 
Echo "! "
 
Pwd
Echo "! Pwd"
 
[Root @ smsgw root] #./test_exclamation_mark.sh
!
/Root
! Pwd
[Root @ smsgw root] #
 
 
$ '...' (ANSI-C Quoting)
As mentioned above, characters enclosed in single quotes do not have special meanings, so single quotes cannot appear in a pair of single quotes. However, after adding $, you can use \ to escape. The meaning of \ is the same as that in C.
Advanced Bash-Scripting Guide: Chapter 3. Special Characters wrote
$ '...'
Quoted string expansion. This construct expands single or multiple escaped octal or hex values into ASCII or Unicode characters.
 
Advanced Bash-Scripting Guide: 5.2. Escaping wrote
The $ '...' quoted string-expansion construct is a mechanic that uses escaped octal or hex values to assign ASCII characters to variables, e.g., quote = $ '\ 042 '.
 
Man bash wrote:
Words of the form $ 'string' are treated specially. The word expands to string, with backslash-escaped charac-
Ters replaced as specified by the ansi c standard. Backslash escape sequences, if present, are decoded as fol-
Lows:
\ A alert (bell)
\ B backspace
\ E an escape character (not ansi c)
\ F form feed
\ N new line
\ R carriage return
\ T horizontal tab
\ V vertical tab
\ Backslash
\ 'Single quote
\ Nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\ XHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\ Cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.
 
Jingle.
[Root @ jfht ~] # Echo $ '\'
 
Jingdang.
[Root @ jfht ~] # Echo $ '\ a \'
 
Single quotes.
[Root @ jfht ~] # Echo $ '\''
'
ASCII characters in octal format.
[Root @ jfht ~] # Echo $ '\ 101 \ 102 \ 103 \ 010'
ABC
[Root @ jfht ~] #
[Root @ jfht ~] #

$ "..." (Locale-Specific Translation)
If $ is added before double quotation marks, this part of content cannot be well understood, so I won't say it. Hope you can give me some advice.
Man bash wrote:
A double-quoted string preceded by a dollar sign ($) will cause
String to be translated according to the current locale. If the cur-
Rent locale is C or POSIX, the dollar sign is ignored. If the string
Is translated and replaced, the replacement is double-quoted.

Author: "Bash @ Linux"
 

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.