I know, this was asked a bilion times, but i yet did not find the optimal solution for my specific case.
I’m receving a string like this:
VAR1="some text here" VAR2='some another text' some script --with --some=args
how do i split the string like this: (most preferable in pure bash)
VAR1="some text here"
VAR2='some another text'
some script --with --some=args
set -- $str result in VAR1="some
set -- "$str" returns entire string
eval set -- "$str" result in VAR1=some text here
sure, i could add quotes to the string returned by eval, but i get highly untrusted input so eval is not a option at all.
Important: there can be from zero to unlimited VARs and they can be single or double quoted
Also, the VAR is a fake name here, it can in fact be anything.
Thanks.
Huh, seems i’m late to the party 🙂
Here is how i’m dealing with environment vars passed before script.
First off all,
escape_argsfunction will escape spaces “inside” passed vars,so if user pass
VAR="foo bar", it will becomeVAR=foo\0040bar.Lets test it against a modified version of your input:
see, all vars are space-escaped and quotes removed, so
declarewill work correctly.Now you can iterate through the parts of your input.
Here is an example how you can declare vars and run the script:
This iterator is doing two simple things:
SOME_VAR=expressionso the output will be:
Is this close to your needs?