Shell $ (), '', $ {}, $ (), and (())

Source: Internet
Author: User

Tag: $ {} ''$ () Shell


  1. $ (), ''For command replacement

  2. $ {} Is used to replace variables (the following content is from shell 13)

(1). Definition of Variables

        $ A=B        $ echo $AB

When executing this command, the AB is regarded as a variable rather than the connection between the variable $ A and the letter B.

        $ A=B        $ echo ${A}B

After the command is executed, the following output is: bb


(2). Special functions are helpful for string processing.

First, define the variable file =/dir1/dir2/dir3/my.file.txt. The following command gets different values.

  • Extract Based on characters in the string: # And %

    • $ {File # */}: remove the first "/" and Its left string->/dir1/dir2/dir3/my.file.txt

    • $ {File ### */}: remove the last "/" and Its left string-> my.file.txt

    • $ {File %/*}: remove the last "/" and Its right string->/dir1/dir2/dir3

    • $ {File %/*}: remove the first "/" and Its right string-> Null Value

  • Extract specified characters in length at a specified position

    • $ {File: 0: 5}: extract the leftmost five characters->/dir1

    • $ {File: 5: 5}: extract 5 consecutive 5 characters from the right of 5th characters->/dir2

  • String replacement

    • $ {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

  • Calculate the length of the variable value: $ {# Var}

    • $ {# File}-> 27 is strlen (file)


(3) array-related


    • A = "a B C Def": the variable value is a string.

    • A = (a B C DEF): defines $ A as an array.



    • $ {A [@]} or $ {A [*]}: obtain all arrays.

    • $ {A [0]}: first element

    • $ {# A [@]} or $ {# A [*]}: Number of array elements

3. $ () is used for calculation.

    $ a=5;b=7    $ echo $((a+b))    12

4. () You can redefine the variable value.

A = 5; (A ++) You can redefine $ A as 6

This article is from the "Linux" blog, please be sure to keep this source http://4983206.blog.51cto.com/4973206/1436474

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.