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?

Biswajit Maharana
  • 531
  • 1
  • 8
  • 23
  • Can you please share some more details about the problem? – Manish Sundriyal Apr 08 '20 at 08: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? – Biswajit Maharana Apr 08 '20 at 08:47
  • You can access parameter using `$1` and `$2` Check this thread --> https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – Digvijay S Apr 08 '20 at 09:05
  • @DigvijayS it's not about accessing it, I want to update $1 $2 with some new values using ```set``` – Biswajit Maharana Apr 08 '20 at 09:08
  • You want to update value of `$publish_pcom_test $publish_svs_test` right ? try `export $publish_pcom_test="Blah blah "` – Digvijay S Apr 08 '20 at 09:11
  • Yes before exporting it, I need to update it to the new values, I have the current values of ```$publish_pcom_test $publish_svs_test``` inside $1 and $2, can I use ```$publish_pcom_test $publish_svs_test``` directly inside script? Like you said ```$publish_pcom_test="Blah blah "``` or I have to do like ```$1="Blah blah"``` – Biswajit Maharana Apr 08 '20 at 09:16

1 Answers1

1

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.

Léa Gris
  • 17,497
  • 4
  • 32
  • 41