110

Is there a way to change the command line arguments in a Bash script? For example, a Bash script is invoked like this:

./foo arg1 arg2  

Is there a way to change the value of arg1 within the script? Something like:

$1="chintz"
Pang
  • 9,073
  • 146
  • 84
  • 117
Sriram
  • 9,924
  • 20
  • 79
  • 136

3 Answers3

172

You have to reset all arguments. To change e.g. $3:

$ set -- "${@:1:2}" "new" "${@:4}"

Basically you set all arguments to their current values, except for the one(s) that you want to change. set -- is also specified by POSIX 7.

The "${@:1:2}" notation is expanded to the two (hence the 2 in the notation) positional arguments starting from offset 1 (i.e. $1). It is a shorthand for "$1" "$2" in this case, but it is much more useful when you want to replace e.g. "${17}".

thkala
  • 80,583
  • 22
  • 150
  • 196
  • 1
    so, in order to change $3, I must change $1 and $2 as well, is it? And change them to what? What does "reset" mean? – Sriram Jan 28 '11 at 11:31
  • Thanks for the trick! I had difficulty using this for filenames with embedded spaces. For anyone else who may run into that problem, try putting `eval` at the front of the line per [this](http://www.linuxjournal.com/content/bash-preserving-whitespace-using-set-and-eval). – cxw Oct 30 '13 at 20:06
  • 2
    This is ok when you know the position of the parameter you want to change. What if you actually don't know it and need to change it dynamically? I tried with $ set -- "${:1:$pivot}" but it doesn't accept a variable there. – Daniele Segato May 09 '14 at 08:16
  • 1
    How can I do the same thing from inside a function? Your solution doesn't seem to work when invoked from inside a function. – Hashken May 06 '15 at 18:14
  • 3
    You cannot change the positional parameters of a script from within a function - the function's own arguments hide them. You can, however, store them in an array and operate on that. – thkala May 06 '15 at 23:11
26

Optimising for legibility and maintainability, you may be better off assigning $1 and $2 to more meaningful variables (I don't know, input_filename = $1 and output_filename = $2 or something) and then overwriting one of those variables (input_filename = 'chintz'), leaving the input to the script unchanged, in case it is needed elsewhere.

johnsyweb
  • 129,524
  • 23
  • 177
  • 239
  • I wanted an approach where I could alter one of the input arguments itself. I needed to do that since I wanted to return a value from the script. The answer suggested by thkala worked well. Thanks for the response!!! – Sriram Feb 11 '11 at 08:00
  • 3
    @Johnsyweb Agreed. For readability sake, yours is the better method. – Dss Mar 18 '14 at 13:14
  • 1
    "You are better off" always depends on the use case, which is not provided. – Him Apr 04 '18 at 19:36
  • 2
    @Scott 7 years older and more experience than I was when I wrote this answer, I'm inclined to agree with you. – johnsyweb Apr 05 '18 at 12:48
  • Example where this would not work (apart from using something like $FIRST_PARAMETER): having the use and meaning of the parameter depending on the actual value. – Igor Stoppa Sep 16 '18 at 13:54
  • @Sriram I cannot imagine a situation where you cannot pass the value of a variable and then return that from a script. Do you mind clarifying in what scenario this answer would not work? – BUFU Oct 26 '20 at 15:31
6

I know this is an old one but I found the answer by thkala very helpful, so I have used the idea and expanded on it slightly to enable me to add defaults for any argument which has not been defined - for example:


    # set defaults for the passed arguments (if any) if not defined.
    #
    arg1=${1:-"default-for-arg-1"}
    arg2=${2:-"default-for-arg-2"}
    set -- "${arg1}" "${arg2}"
    unset arg1 arg2

I hope this is of use to someone else.

mr_thinkit
  • 71
  • 1
  • 7