1

I'm executing a shell script and passing few command line arguments to it.

I want to modify the arguments inside the script using set. Not all at once depending upon some conditions.

How can I do that?

6
  • Can you please share some more details about the problem? Commented Apr 8, 2020 at 8:44
  • ./check_test_results.sh $publish_pcom_test $publish_svs_test This is how I'm executing the script. The two arguments are having some default values(True/False). I want to do some operations and want to decide whether I have to modify these argument values or not. If I have to update these values then how can I do that? Commented Apr 8, 2020 at 8:47
  • You can access parameter using $1 and $2 Check this thread --> stackoverflow.com/questions/192249/… Commented Apr 8, 2020 at 9:05
  • @DigvijayS it's not about accessing it, I want to update $1 $2 with some new values using set Commented Apr 8, 2020 at 9:08
  • You want to update value of $publish_pcom_test $publish_svs_test right ? try export $publish_pcom_test="Blah blah " Commented Apr 8, 2020 at 9:11

1 Answer 1

2

Copy unmodified arguments at their respective location within set --

Say you want to modify value of argument 2:

set -- "${@::2}" 'new arg2 value' "${@:3}"

Explanation:

  • "${@::2}": Expands 2 arguments from index 0 (arguments 0 and 1)
  • new arg2 value: Becomes the value for argument 2.
  • "${@:3}": Expands all argument values starting at index 3.

Opinion:

Anyway, having mutable arguments is considered code-smell in modern programming. So I'd recommend you reconsider your approach to the problem you are trying to solve.

Sign up to request clarification or add additional context in comments.

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.