Gerhard
Gerhard

Reputation: 407

ZSH: splitting a string at quotation marks

Is there a way to split a string at quotation marks?

I tried this code, but it doesn't split the string at the quotation mark.

STRING='imgdir="/path/to/my/images"'
VAR=${$(s/\"/)STRING}

I have a file that contains data in the following format. I'd like to easily access the values in quotation marks.

imgdir="/path/to/my/images"
imgdir2="/path/to/my/images2"
testvalue="554"

Upvotes: 2

Views: 472

Answers (2)

Jens
Jens

Reputation: 72639

If the file only contains assignments as shown (and no non-shell syntax), just source it:

. /path/to/file

Then you have all string values in the shell variables imgdir, imgdir2, testvalue, ..., with the quotes removed.

EDIT: If there's non-shell syntax, grep for the assignments first:

grep -E '[a-z0-9_]+=".*"' > assignments
. ./assignments
rm assignments

Upvotes: 1

Gerhard
Gerhard

Reputation: 407

okay, answering my own question:

eval $(grep "imgdir=" $FILE)

does the trick for what I want to do in this case: It assigns a variable "imgdir" with what is between the quotation marks...

The original question remains though: how to split a string at quotation marks...

/Gerhard

Upvotes: 0

Related Questions