I have an indexed bash array and I'd like to use an expression like "${a[@]}" except I want it to not include a[0]. The best that I can think of is this:
j=0 for i in "${a[@]}" do b[j]=${a[++j]} done
and then use "${b[@]}". Is there a better way?
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
To get the first element (10) from the array, we can use the subscript [ ] syntax by passing an index 0 . In bash arrays are zero-indexed, so the first element index is 0 .
bash arrays start at index position 0.
$ a=(1 2 3) $ echo "${a[@]:1}" 2 3
If it's a standard array, use:
"${a[@]:1}"
If you're working with parameters:
"${@:2}"
Note the different syntax and that $@
is 1-indexed (since $0 is the name of the script).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With