Skip to main content
added 4020 characters in body
Source Link

More array slicing examples in bash

In place of using some_array_variable in your code (argv in your case), simply use @ in that variable name's place. The symbol @ represents the input argument array, and you can treat it exactly like you'd treat any other array. The confusion is simply that we are accustomed to seeing it almost always paired with the $ character like this: $@, and so we don't realize that the @ character is the array itself, and can be used alone as an array variable as well.

So, here are some examples:

Slice from the input argument array, @, which is normally seen and accessed simply as $@:

# array slicing basic format 1: grab a certain length starting at a certain
# index
echo "${@:2:5}"
#         │ │
#         │ └────> slice length
#         └──────> slice starting index 

# array slicing basic format 2: grab all remaining array elements starting at a
# certain index through to the end
echo "${@:2}"
#         │
#         │
#         └──────> slice starting index 

More array reminders to myself:

The following are general array reminders to myself that may benefit others landing on this page too.

# store a slice from an array into a new array
new_array=("${@:4}")

# print the entire array (use "*" to generate a space-separated list
# of elements, not "@" which would generate separate arguments instead)
echo "new_array = ${new_array[*]}"

Here is a runnable example of generic array slicing and array element access in bash, inspired by this source:

(Use @ as the input argument array if desired, instead of a below, according to your use case)

a=(one two three four five six)   # define a new array, `a`, with 6 elements
echo "$a"           # print first element of array a
echo "${a}"         # print first element of array a
echo "${a[0]}"      # print first element of array a
echo "${a[1]}"      # print *second* element of array a
echo "${#a[@]}"     # print number of elements in array a
echo "${a[@]:1:3}"  # print 2nd through 4th elements; ie: the 3 elements
                    # starting at index 1, inclusive, so: indices 1, 2, and 3
                    # (output: `two three four`)
echo "${a[@]:1}"    # print 2nd element onward

Run it all

Copy and paste all code chunks above into a file called array_slicing_demo.sh, and mark it executable with chmod +x array_slicing_demo.sh so that you can run it. Or, just download my demo array_slicing_demo.sh file from my eRCaGuy_hello_world repo here.

Then, run it like this:

./array_slicing_demo.sh a b c d e f g h i j k

...and you will see the following output:

b c d e f
b c d e f g h i j k
new_array = d e f g h i j k
one
one
one
two
6
two three four
two three four five six

Examples of echo "${a[@]}" vs echo "${a[*]}"

* does what we want here, whereas @ does not. See help echo for its arguments.

# Make an array, a
$ a=(-e "\n" there)

# This outputs what we want
$ echo "${a[*]}"
-e \n there

# This does not. Instead, it passes `-e` and `\n` as separate arguments to echo,
# interpreting `-e` as the "escape" option to echo.
$ echo "${a[@]}"

 there

References

  1. Bash: slice of positional parameters
  2. Single parenthesis in bash variable assignment

Keywords: array access in bash; bash array indexing; access elments in arrays in bash; bash array slicing; print arrays in bash; print array elements in bash

More array slicing examples in bash

In place of using some_array_variable in your code (argv in your case), simply use @ in that variable name's place. The symbol @ represents the input argument array, and you can treat it exactly like you'd treat any other array. The confusion is simply that we are accustomed to seeing it almost always paired with the $ character like this: $@, and so we don't realize that the @ character is the array itself, and can be used alone as an array variable as well.

So, here are some examples:

Slice from the input argument array, @, which is normally seen and accessed simply as $@:

# array slicing basic format 1: grab a certain length starting at a certain
# index
echo "${@:2:5}"
#         │ │
#         │ └────> slice length
#         └──────> slice starting index 

# array slicing basic format 2: grab all remaining array elements starting at a
# certain index through to the end
echo "${@:2}"
#         │
#         │
#         └──────> slice starting index 

More array reminders to myself:

The following are general array reminders to myself that may benefit others landing on this page too.

# store a slice from an array into a new array
new_array=("${@:4}")

# print the entire array (use "*" to generate a space-separated list
# of elements, not "@" which would generate separate arguments instead)
echo "new_array = ${new_array[*]}"

Here is a runnable example of generic array slicing and array element access in bash, inspired by this source:

(Use @ as the input argument array if desired, instead of a below, according to your use case)

a=(one two three four five six)   # define a new array, `a`, with 6 elements
echo "$a"           # print first element of array a
echo "${a}"         # print first element of array a
echo "${a[0]}"      # print first element of array a
echo "${a[1]}"      # print *second* element of array a
echo "${#a[@]}"     # print number of elements in array a
echo "${a[@]:1:3}"  # print 2nd through 4th elements; ie: the 3 elements
                    # starting at index 1, inclusive, so: indices 1, 2, and 3
                    # (output: `two three four`)
echo "${a[@]:1}"    # print 2nd element onward

Run it all

Copy and paste all code chunks above into a file called array_slicing_demo.sh, and mark it executable with chmod +x array_slicing_demo.sh so that you can run it. Or, just download my demo array_slicing_demo.sh file from my eRCaGuy_hello_world repo here.

Then, run it like this:

./array_slicing_demo.sh a b c d e f g h i j k

...and you will see the following output:

b c d e f
b c d e f g h i j k
new_array = d e f g h i j k
one
one
one
two
6
two three four
two three four five six

Examples of echo "${a[@]}" vs echo "${a[*]}"

* does what we want here, whereas @ does not. See help echo for its arguments.

# Make an array, a
$ a=(-e "\n" there)

# This outputs what we want
$ echo "${a[*]}"
-e \n there

# This does not. Instead, it passes `-e` and `\n` as separate arguments to echo,
# interpreting `-e` as the "escape" option to echo.
$ echo "${a[@]}"

 there

References

  1. Bash: slice of positional parameters
  2. Single parenthesis in bash variable assignment

Keywords: array access in bash; bash array indexing; access elments in arrays in bash; bash array slicing; print arrays in bash; print array elements in bash

Fix a very common mistake in the "echo entire array" example
Source Link
# store a slice from an array into a new array
new_array=("${@:4}")

# print the entire array (use "*" to generate a space-separated list
# of elements, not "@" which would generate separate arguments instead)
echo "new_array = ${new_array[@]new_array[*]}"
# store a slice from an array into a new array
new_array=("${@:4}")

# print the entire array
echo "new_array = ${new_array[@]}"
# store a slice from an array into a new array
new_array=("${@:4}")

# print the entire array (use "*" to generate a space-separated list
# of elements, not "@" which would generate separate arguments instead)
echo "new_array = ${new_array[*]}"
added 665 characters in body
Source Link

Run it all

SampleCopy and paste all code chunks above into a file called array_slicing_demo.sh, and mark it executable with chmod +x array_slicing_demo.sh so that you can run it. Or, just download my demo array_slicing_demo.sh file from my eRCaGuy_hello_world repo here.

Then, run it like this:

./array_slicing_demo.sh a b c d e f g h i j k

...and you will see the following output:

b c d e f
b c d e f g h i j k
new_array = d e f g h i j k
one
one
one
two
6
two three four
two three four five six

Sample output:

one
one
one
two
6
two three four
two three four five six

Run it all

Copy and paste all code chunks above into a file called array_slicing_demo.sh, and mark it executable with chmod +x array_slicing_demo.sh so that you can run it. Or, just download my demo array_slicing_demo.sh file from my eRCaGuy_hello_world repo here.

Then, run it like this:

./array_slicing_demo.sh a b c d e f g h i j k

...and you will see the following output:

b c d e f
b c d e f g h i j k
new_array = d e f g h i j k
one
one
one
two
6
two three four
two three four five six
added 108 characters in body
Source Link
Loading
added 172 characters in body
Source Link
Loading
added 172 characters in body
Source Link
Loading
Source Link
Loading