Shell-related knowledge

Source: Internet
Author: User

In the bash Shell , $ () and "(anti-quote) are used for command substitution.
The so-called command substitution is similar to the variable substitution we learned in the fifth chapter, which is used to reorganize the command line:
* Complete the command line in quotation marks, then replace the result, then reorganize the command line.
For example:

$ echo the last Sunday is $ (date -D "last Sunday" +%y-%m-%d)

In the operation, with $ () or "It doesn't matter, just my" personal "prefer to use $ (), the reason is:
1, ' It's easy to mess with ' (single quotes), especially for beginners.
Sometimes in some strange glyphs, the two symbols are identical (vertical two points).
Of course, an experienced friend can make a difference at a glance. But what if it is better to avoid confusion and he le?
2, in the multi-level compound substitution, ' need extra to take off (/') processing, and $ () is more intuitive. For example:
This is wrong:

Command1 ' Command2 ' Command3 '

The original intention is to Command2 ' Command3 ' first to change the Command3 to command 2 processing, and then pass the results to Command1 ' Command2 ... ' to deal with.
However, the real results are divided into ' command2 ' and ' two segments ' in the command line.
The correct input should be as follows:

Command1 ' Command2/' command3/'

Otherwise, a $ () is no problem:

Command1 $ (Command2 $ (COMMAND3))

As long as you like, how many layers of replacement are no problem ~ ~ ~ ^_^

However, $ () is not without drawbacks ...
First of all, ' basically used in all of the Unix shell, if written in shell script, its portability is relatively high.
and $ () every shell can be used, I can only tell you, if you use bash2 words, certainly no problem ...

Next, let's see ${} ... It's actually used for variable substitution .
In general, $var is not the same as ${var}.
But using ${} will be more precise in defining the range of variable names, for example:

$ a=b
$ echo $AB

Originally intended to replace the result of a $ A, and then fill a B letter after it, but on the command line, the real result is only replace the value of the variable name AB ...
If you use ${} There is no problem:

$ echo ${a}b
Bb

However, if you only see ${} can only be used to define the variable name, then you really underestimate bash!
If you are interested, you can first refer to the essence of the CU version of the article:
http://www.chinaunix.net/forum/viewtopic.php?t=201843
Refer to Previous: http://www.cnblogs.com/youxin/p/3544179.html


For the sake of completeness, I'll use some examples here to illustrate some of the supernatural powers of ${}:
Suppose we define a variable as:
File=/dir1/dir2/dir3/my.file.txt
We can replace each other with ${} to get different values:
${file#*/}: Take out the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Take out the last/and left string: my.file.txt
${file#*.}  : Take out the first one. And the string to the left: file.txt
${file##*.}  : Take out the last one. And the string to the left: txt
${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3
${file%%/*}: Remove the first/its right string: (null value)
${FILE%.*}: Take off the last one. And the string to the right:/dir1/dir2/dir3/my.file
${FILE%%.*}: Take out the first one. And the string to the right:/dir1/dir2/dir3/my
The methods of memory are:

#是去掉左边 (on the plate # on the left of $)
% is removed to the right (on the plate% on the right of the $)
The single symbol is the minimum match, and the two symbol is the maximum match.

Get substring:

${file:0:5}: Extract the leftmost 5 bytes:/dir1
${file:5:5}: Extracts the 5th byte to the right of 5 consecutive bytes:/DIR2

We can also replace the string in the value of the variable:
${file/dir/path}: Replace the first dir with a path:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Replace all dir with Path:/path1/path2/path3/my.file.txt

Get file name:

BaseName

Basename-strip directory and suffix from filenames

Synopsis
BaseName NAME [SUFFIX]
BaseName OPTION ... NAME.

Basename/usr/bin/sort---> "sort"

Remove suffix:

[Email protected]:~# basename ms.tgz. tgz

Ms

Shell-related knowledge

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.