Shell variables and shell Variables

Source: Internet
Author: User

Shell variables and shell Variables

Suppose we define a variable:
File =/dir1/dir2/dir3/my.file.txt

You can replace $ {} with different values:
$ {File # */}: Delete the first/and its left string: dir1/dir2/dir3/my.file.txt
$ {File ### */}: Delete the last/and its left string: my.file.txt
$ {File # *.}: Delete the first. And its left string: file.txt
$ {File ### *.}: Delete the last. And its left string: txt
$ {File %/*}: Delete the last/and its right string:/dir1/dir2/dir3
$ {File %/*}: deletes the first/and its right string: (null)
$ {File %. *}: Delete the last. And its right string:/dir1/dir2/dir3/my. file
$ {File %. *}: Delete the first. And its right string:/dir1/dir2/dir3/my

 

The memory is as follows:
# Remove the left side (on the keyboard # on the left side of $)
% Is to remove the right side (% on the keyboard is on the right side of $)
A single symbol is the minimum match; two symbols are the maximum match.
$ {File: 0: 5}: extract the leftmost five Bytes:/dir1
$ {File: 5: 5}: extract 5 consecutive bytes from the right of 5th Bytes:/dir2

 

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

 

 

You can use $ {} to assign values to different variable states (not set, null, or non-null ):
Optional bytes file-my.file.txt}: If $ file is not set, use my.file.txt as the return value. (Null and non-null values are not processed)
$ {File: -my.file.txt}: If $ file is not set or is null, use my.file.txt as the callback value. (Not processed when the value is not null)
Optional values file1_my.file.txt}: If $ file is set to a null or non-null value, my.file.txt is used as the callback return value. (Not handled at the specified time)
$ {File: Invalid my.file.txt}: If $ file is not null, use my.file.txt as the callback value. (When no value is set or the null value is set, it is not processed)
When using file1_my.file.txt}: If $ file is not set, use my.file.txt as the response value, and set $ file to my.file.txt at the same time. (Null and non-null values are not processed)
$ {File: Invalid my.file.txt}: If $ file is not set or is null, use my.file.txt as the callback value. At the same time, set $ file to my.file.txt. (Not processed when the value is not null)
$ {File? My.file.txt}: If $ file is not set, export my.file.txt to STDERR. (Null and non-null values are not processed)
 

$ {File :? My.file.txt}: If $ file is not set or is null, output my.file.txt to STDERR. (Not processed when the value is not null)

The length of the variable value can be calculated from $ {# var:
 

$ {# File} can get 27 because/dir1/dir2/dir3/my.file.txt is 27 bytes.


How does shell execute statements in variables?

This is related to the shell used.
Bash:
Test = 'echo "hello world "'
$ Test

Zsh needs to be like this:
Test = 'echo "hello world "'
$ {= Test}

Two references to a shell variable

Write the variable name directly to sed.
Sed-I "s/address. */address $ variable name/"/root/1

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.