How to get the first part of a string in bash

Let's say I have the following file names from ls in a bash script:

 things-hd-91-Statistics.db things.things_domain_idx-hd-38-Data.db 

In bash, how could he get the first part of the string 'things' anyway? Basically delete the rest of the line after the first - or .

+4
source share
1 answer

You used the parameter extension :

 string="things-hd-91-Statistics.db" echo "${string%%-*}" things 

Where in ${parameter%%pattern} "pattern" ( -* ) matches the end of the "parameter". The result is an extended parameter value with the longest match.

Similarly for your other example, the pattern will be %%.*

+13
source

Source: https://habr.com/ru/post/1438221/


All Articles