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

  • Home
  • SEARCH
  • 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 7668957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:29:17+00:00 2026-05-31T15:29:17+00:00

There is this code: test2.cpp (compiled to test2 executable): #include <iostream> int main(int argc,

  • 0

There is this code:

test2.cpp (compiled to test2 executable):

#include <iostream>

int main(int argc, char** argv)
{
   for(int i = 0; i < argc; ++i){
      std::cout << i << " " << argv[i] << std::endl;
   }
   return 0;
}

test.sh (parameters preparing script):

some_array=("text1" "text two" "text tree")

input_variables=""

for i in $(seq 0 $((${#some_array[*]} - 1))); do
   input_variables="$input_variables --parameter=\"${some_array[$i]}\""
   echo $input_variables
done

echo "Running: ./test2$input_variables"
./test2 $input_variables

Output:

$ sh test.sh
--parameter="text1"
--parameter="text1" --parameter="text two"
--parameter="text1" --parameter="text two" --parameter="text tree"
Running: ./test2 --parameter="text1" --parameter="text two" --parameter="text tree"
0 ./test2
1 --parameter="text1"
2 --parameter="text
3 two"
4 --parameter="text
5 tree"

I would like to pass to test2 executable 3 arguments but 5 arguments are passed and there is a problem with space character between words. I would like also to keep input parameters in array like it is presented in *some_array* so I can add there something whenever I want. How to pass these parameters correctly?

  • 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-05-31T15:29:18+00:00Added an answer on May 31, 2026 at 3:29 pm

    Embedding quotes (or escapes) in a variable’s value doesn’t do anything useful. Quotes are parsed before variables are replaced, so by the time the quotes get there it’s too late for them to do anything (see BashFAQ #050 for more examples). And BTW using echo to print a command is extremely misleading, use set -x to have the shell print what it thinks is going on.

    The best thing to do, as you said in your final note, is to store the parameters in an array:

    #!/bin/bash
    some_array=("text1" "text two" "text tree")
    
    input_variables=()
    
    for i in $(seq 0 $((${#some_array[*]} - 1))); do
       input_variables+=("--parameter=${some_array[$i]}")
       echo "${input_variables[@]}"
    done
    
    printf "Running:"
    printf " %q" ./test2 "${input_variables[@]}"
    ./test2 "${input_variables[@]}"
    

    BTW, arrays are a bash extension, and not always available in plain shells, so you should run this with bash, not sh.

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

Sidebar

Related Questions

Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
I am trying to compile the following program: #include <iostream> int main(){ std::cout <<
I've been looking at C++0x threads and have this code: #include <vector> #include <iostream>
There is this code: class SomeClass { public: SomeClass(){} SomeClass(SomeClass& b){} SomeClass(SomeClass&b, SomeClass& c){}
Is there anyway this code can be refactored? The only difference is the order
There is currently this Prototype code that does a PUT: new Ajax.Request(someUrl, { method:
Is there anything wrong with this code? My entity is not getting updated. public
I have this code so far which perfectly but relies on there being a
Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String

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.