2

I wanted to pass 2 fixed and remaining 'n' number of arguments to the script which I wanted to store in an array variable. Could someone please suggest how to achieve this via code. Example:

sh myScript.sh fixedArgument1 fixedArgument2 var1 var2 ... varN

N can be anything but not more than 15.

Also, I can get the value of fixedArgument1 in script via $1 and fixedArgument2 $2 but how to put the remaining arguments in array variable.

10
  • Please add to your question (no comment): What have you searched for, and what did you find? What have you tried, and how did it fail? Commented Jan 9, 2022 at 15:04
  • Which shell do you use? Commented Jan 9, 2022 at 15:09
  • The Bourne shell ( sh ) Commented Jan 9, 2022 at 15:11
  • The Bourne shell does not know arrays. Commented Jan 9, 2022 at 15:26
  • @cyrus since the question is specifically about arrays, it seems more appropriate to keep the bash tag and remove the sh tag. Commented Jan 9, 2022 at 15:28

2 Answers 2

3

Just shift off the fixed position arguments and get the remainder with "$@". eg:

#!/bin/bash

echo first arg: "$1"
echo 2nd arg: "$2"
shift
shift
array=("$@")
for element in "${array[@]}"; do
    echo "$element"
done

Make sure you're using a shell that supports arrays. sh may not, as /bin/sh is often not bash.

But note that it sounds like you are probably working too hard. Why bother putting the arguments in an array at all? You can access them via $@ just as easily as you will access them from the array, and it probably makes more sense to do that. The array is probably just going to obfuscate the code. For example:

#!/bin/sh

echo "first arg: '$1'"
echo "2nd arg: '$2'"
shift 2  # discard the first two arguments
if test $# -gt 0; then
    echo There are $# arguments remaining:
    i=1
    # Iterate over the remaining arguments
    for x; do echo "arg $((i++)): '$x'"; done
fi
Sign up to request clarification or add additional context in comments.

5 Comments

I have not used $n before and how we can know how many exact number of arguments are there. Can you pls share an example if possible.
$# tells you the number of arguments. My $n was an attempt to generically refer to $1, $2, etc. You can iterate over all the arguments by inspecting $@. The common idiom is for x in "$@"; do ...; done, which is so common that you can abbreviate it with for x; do ...; done
thanks for detailed description. It looks like better option to do. I will follow the same and will let you know. Thanks.
Instead of shift; shift, you can also shift 2
@LéaGris Thanks! I always forget about that.
2
#!/bin/bash

function main {
    local arg1=$1 arg2=$2 remaining=("${@:3}")
    ...
}

main "$@"

Also as suggested already, you don't need to store remaining arguments to an array, and you can access it directly either through "${@:3}", or through "$@" after executing shift 2. In a for loop the in ... part can be ignored if target is "$@" and positional parameters aren't modified inside the loop through set -- since this may or may not affect the loop arguments depending on implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.