1. Overview

Changing command-line arguments in Bash can be useful when testing for varying input. By changing these arguments, we can test different configurations and observe the resulting outcomes without having to manually enter values. This is particularly useful during the development and debugging phases, allowing us to quickly experiment with various scenarios.

In this tutorial, we’ll explore how we can change command-line arguments in the Bash shell.

2. Changing Command-Line Arguments

Command-line arguments are values passed to a script or command when it’s executed. In Bash, these arguments are accessible through the special variables $1, $2, $3, up to $9. In this case, $1 represents the first argument, $2 represents the second argument, and so forth. These arguments are also contained in the argument array, represented by the special variable $@.

The set command enables us to change command-line arguments in a flexible way. By using with set, we can assign a new value for each argument. When performing this task, we have a couple of options:

  • explicitly specify the set of arguments
  • combine existing arguments with new ones

Let’s start by setting a list of command-line arguments:

$ set -- arg1 arg2 arg3 arg4 arg5 arg6

Here, we assign six positional arguments with the set — command. We can view the current arguments and their values via $@:

$ echo "$@"
arg1 arg2 arg3 arg4 arg5 arg6

To change the fourth argument, we can reset the argument list while also making use of array slicing:

$ set -- "${@:1:3}" "new_arg4" "${@:5}"
$ echo "$@"
arg1 arg2 arg3 new_arg4 arg5 arg6

Again, set assigns new values to the positional parameters, effectively changing the command-line arguments. In particular, ${@:1:3} retrieves the values of the first three positional parameters, which are arg1, arg2, and arg3. The ${@:1:3} syntax extracts a subset starting from position 1 and having a length of 3. The result of ${@:1:3} based on the values in $@ is a list of three elements:

  1. arg1
  2. arg2
  3. arg3

Then, we assign the value new_arg4 as the fourth positional parameter. Finally, ${@:5} retrieves all the positional parameters from the fifth position until the end. The result is a list containing all the parameters from arg5 onward.

To remove the fourth positional argument, we can re-list the arguments as desired, skipping the fourth element in $@:

$ set -- "${@:1:3}" "${@:5}"
$ echo "$@"
arg1 arg2 arg3 arg5 arg6

In general, by using array slicing and concatenating elements, we can rearrange the set of arguments, collectively, in any way desired.

3. Using Variables for Command-Line Arguments

A common approach to changing command-line arguments within a script is to assign them to variables. Therefore, instead of directly modifying the arguments, we copy their values in variables and modify these variables. Thus, we can manipulate the arguments without affecting the original command-line argument variables and their values. In fact, this approach of preserving the original script and leaving its arguments intact is considered a best practice.

Let’s illustrate the idea with a script, named modify_args.sh:

$ cat modify_args.sh
#!/usr/bin/env bash

# Modify the variables

echo "Unmodified arguments: $@"
echo "Modified first argument: $arg1"
echo "Modified second argument: $arg2"

We assign the first two arguments passed to the script to the variables arg1 and arg2, respectively. Then, these variables are modified, and the original arguments as well as the new values are echoed by the script.

Next, we grant the script execute permissions via chmod and run it with two positional arguments:

$ chmod +x modify_args.sh
$ ./modify_args.sh one two
Unmodified arguments: one two
Modified first argument: new_arg1
Modified second argument: new_arg2

We notice that the unmodified arguments are one and two, while variables arg1 and arg2 have taken on the new values assigned in the script. This is a common way to modify and use command-line arguments while preserving the original versions passed on to the script.

4. Conclusion

In this article, we’ve explored how to change command-line arguments in Bash via the set command and using variables within a script. By understanding the concepts of positional parameters, the argument array, and string manipulation capabilities, we can effectively adapt the behavior of scripts and commands to suit our requirements.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.