How to extract last part of string in bash?

后端 未结 6 1529
谎友^
谎友^ 2020-12-28 12:39

I have this variable:

A="Some variable has value abc.123"

I need to extract this value i.e abc.123. Is this possible i

6条回答
  •  失恋的感觉
    2020-12-28 13:15

    The documentation is a bit painful to read, so I've summarised it in a simpler way.

    Note that the '*' needs to swap places with the '' depending on whether you use # or %. (The * is just a wildcard, so you may need to take off your "regex hat" while reading.)

    • ${A%% *} - remove longest trailing * (keep only the first word)
    • ${A% *} - remove shortest trailing * (keep all but the last word)
    • ${A##* } - remove longest leading * (keep only the last word)
    • ${A#* } - remove shortest leading * (keep all but the first word)

    Of course a "word" here may contain any character that isn't a literal space.

提交回复
热议问题