Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:51:45+00:00 2026-06-13T01:51:45+00:00

I know, this was asked a bilion times, but i yet did not find

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:51:47+00:00Added an answer on June 13, 2026 at 1:51 am

    Huh, seems i’m late to the party 🙂

    Here is how i’m dealing with environment vars passed before script.

    First off all, escape_args function will escape spaces “inside” passed vars,

    so if user pass VAR="foo bar", it will become VAR=foo\0040bar.

    function escape_args {
      local str=''
      local opt=''
      for c in $1; do
        if [[ "$c" =~ ^[[:alnum:]]+=[\"|\'] ]]; then
          if [[ "${c: -1}" =~ [\"|\']  ]]; then
            str="$str $( echo $c | xargs )"
          else
            # first opt chunk
            # entering collector
            opt="$c"
          fi
        else
          if [ -z "$opt" ]; then
            # not inside collector
            str="$str $c"
          else
            # inside collector
            if [[ "${c: -1}" =~ [\"|\']  ]]; then
              # last opt chunk
              # adding collected chunks and this last one to str
              str="$str $( echo "$opt\0040$c" | xargs )"
              # leaving collector
              opt=''
            else
              # middle opt chunk
              opt="$opt\0040$c"
            fi
          fi
        fi
      done
      echo "$str"
    }
    

    Lets test it against a modified version of your input:

    s="VAR1=\"some text here\" VAR2='some another text' VAR3=\"noSpaces\" VAR4='noSpacesToo' VAR5=noSpacesNoQuotes some script --with --some=args"
    
    echo $(escape_args "$s")
    
    VAR1=some\0040text\0040here VAR2=some\0040another\0040text VAR3=noSpaces VAR4=noSpacesToo VAR5=noSpacesNoQuotes some script --with --some=args
    

    see, all vars are space-escaped and quotes removed, so declare will 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:

    cmd=''
    for c in $(escape_args "$s"); do
      [[ "$c" =~ ^[[:alnum:]]+= ]] && declare "$(echo -e $c)" && continue
      cmd="$cmd $c"
    done
    
    echo VAR1 is set to $VAR1
    echo VAR2 is set to $VAR2
    echo VAR3 is set to $VAR3
    echo VAR4 is set to $VAR4
    echo VAR5 is set to $VAR5
    echo $cmd
    

    This iterator is doing two simple things:

    • declaring a var if the chunk matching SOME_VAR= expression
    • adding the chunk to the final cmd otherwise

    so the output will be:

    VAR1 is set to some text here
    VAR2 is set to some another text
    VAR3 is set to noSpaces
    VAR4 is set to noSpacesToo
    VAR5 is set to noSpacesNoQuotes
    some script --with --some=args
    

    Is this close to your needs?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has been asked many times but i'm yet to find a
I know this has been asked different ways several times, but I'm just not
I know this has been asked thousands of times but I just can't find
I know this has been asked many times, but I cant find answer to
I know this is asked many times before but it s not what i
I know this has been asked many times, but not quite the way I'm
I know this question was asked many times, but I didn't find my situation:
I know this questions being asked many times already but I can not make
I know this has been asked at least a thousand times but I can't
I know this question was asked here many times before but I am still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.