0

Below line is the sample of my code

command mycommand /path/location arg1="my valu1" arg2="my value2"

when I am executing this command by hard-coding the argument value it works.

But when I am using below code format:

for i in "$@" do
str+="$i " 
done
command mycommand /path/location $str

where $str="arg1="my valu1" arg2="my value2""

It is not accepting "my value1" as a single string. It is taking "my" as one string and "value2" as a separate string.

Now if I use

command mycommand /path/location "$str"

then $str="arg1="my valu1" arg2="my value2"" as only one complete string. But I want command should execute in below format through program way.

command mycommand /path/location arg1="my valu1" arg2="my value2"

How to do this?

2 Answers 2

1

Don't try to append your arguments into a string just pass along "$@" to your actual command inside the script:

command mycommand /path/location "$@"
Sign up to request clarification or add additional context in comments.

4 Comments

I have multiple arguments but I want to use some specific set of arguments. so $@ will contain all the arguments which is not applicable for me
Your own code for i in "$@" do is appending every argument to str
This is what I am doing. But getting the mentioned problem.
That's not the point, You showed a code that is trying to append every argument to str but now commenting I want to use some specific set of arguments so there is a mismatch.
0

In a shell which supports arrays (eg, bash), you would use an array:

command mycommand /path/location "${arr[@]}"

If your shell doesn't support arrays, you can use the positional paramters:

command mycommand /path/location "${@}"

A common hack is to use set reset the positional parameters. That is, you can do:

set -- "arg one" "arg two"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.