Bash replace function parameters

Shift and unshift a bash function's parameters by creating a new variable to hold the arguments passed to the function.

debug_command() {
    echo " arg 1: ${args[0]}"
    echo " arg 2: ${args[1]}"
    echo " arg 3: ${args[2]}"

my_func() {

    echo "original args:"
    debug_command "${args[@]}"

    # Replace first argument (at index 0).

    echo "modified args:"
    debug_command "${args[@]}"

my_func "first" "2nd" "and third"
$ bash
original args:
arg 1: first
arg 2: 2nd
arg 3: and third
modified args:
arg 1: changed!
arg 2: 2nd
arg 3: and third

kw: bash, shift, unshift

View this page on GitHub.
Posted .


Leave a Reply