codec
codec

Reputation: 8796

Stripping a string with a another string shell

I am running a shell script and I have the following string:

keystore_location="/mnt/blumeta0/db2/keystore/keystore.p12" How do I fetch string before keystore: i.e /mnt/blumeta0/db2. I know how to strip on a single character delimiter and the path before keystore can change. I tried:

arrIN=(${keystore_location//\"keystore\"/ })

Upvotes: 0

Views: 42

Answers (2)

oguz ismail
oguz ismail

Reputation: 50750

$ keystore_location="/mnt/blumeta0/db2/keystore/keystore.p12"
$ echo "${keystore_location%%/keystore*}"
/mnt/blumeta0/db2

%%/keystore* removes the longest suffix matching /keystore* -which is a glob pattern- from $keystore_location.

Upvotes: 2

shellter
shellter

Reputation: 37258

You want

arrIN=${keystore_location%%keystore*}
echo $arrIn
/mnt/blumeta0/db2/

The %% operator removes the longest match, reading from the right side of the string

Note that there are also operators

%   --- remove first match from the right side of the string
#   --- remove first match starting from the left side of the string
##  --- remove longest match starting for the left side of the string.

IHTH

Upvotes: 2

Related Questions