Linux: Change the Value of an Argument/Parameter in Shell Script

In Linux/Unix, use set command to change the value of an argument/parameter in a shell script.

The arguments we pass to a shell script can be read using the shell variables $1, $2, depends on how many parameters you passed. Here you will learn how to change/assign a new value to these shell variables using the set command.

SET Command Example

The following is a shell script example, in which we will pass the two arguments, and it will print that argument values. Then the set command will change the value of these arguments and even create a new third argument.

echo $1
echo $2
# assign a new value for argument $1 and $2 and create a new third argument
set abc 123 xyz
echo $1
echo $2
echo $3

Test by passing two arguments (1. hello, 2. world)

./ hello world


You can see below that first it printed the argument values you passed to the shell script, then it printed the newly assigned values to the arguments.


See also:

1 thought on “Linux: Change the Value of an Argument/Parameter in Shell Script”

  1. hello

    i am trying to change all arguments / with _ but cannot , at a time only one value get change, how to change all

    #set $1 = echo "$1" | tr '/' '_'
    set $1 = echo "$1" | tr '/' '_' ; $2 = echo "$2" | tr '/' '_'


Leave a Comment