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

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

  • Linux
  • 1 min read

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.

setpar.sh

#!/bin/bash
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)

./setpar.sh hello world

Output

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.

hello
world
abc
123
xyz

See also:

This Post Has One Comment

  1. dont ask

    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 '/' '_'

Comments are closed.