Skip to main content
added 69 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

Note that arrays in bash (copied from ksh) are rather associative arrays (with keys limited to positive integers also called sparse arrays).

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that.

If you wanted to insert the newvalue as ${a[0]} and shift all the other keys by one, you'd need a temporary array:

b=newvalue
for k in "${!a[@]}"; do
  b[k+1]=${a[k]}
done
unset a
for k in "${!b[@]}"; do
  a[k]=${b[k]}
done
unset b

Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variables)

  • yash:

      array -i a 0 newvalue
    

Note that arrays in bash (copied from ksh) are rather associative arrays.

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that.

If you wanted to insert the newvalue as ${a[0]} and shift all the other keys by one, you'd need a temporary array:

b=newvalue
for k in "${!a[@]}"; do
  b[k+1]=${a[k]}
done
unset a
for k in "${!b[@]}"; do
  a[k]=${b[k]}
done
unset b

Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variables)

  • yash:

      array -i a 0 newvalue
    

Note that arrays in bash (copied from ksh) are rather associative arrays (with keys limited to positive integers also called sparse arrays).

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that.

If you wanted to insert the newvalue as ${a[0]} and shift all the other keys by one, you'd need a temporary array:

b=newvalue
for k in "${!a[@]}"; do
  b[k+1]=${a[k]}
done
unset a
for k in "${!b[@]}"; do
  a[k]=${b[k]}
done
unset b

Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variables)

  • yash:

      array -i a 0 newvalue
    
added 278 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

Note that arrays in bash (copied from ksh) are rather associative arrays.

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that.

If you wanted to insert the newvalue as ${a[0]} and shift all the other keys by one, you'd need a temporary array:

b=newvalue
for k in "${!a[@]}"; do
  b[k+1]=${a[k]}
done
unset a
for k in "${!b[@]}"; do
  a[k]=${b[k]}
done
unset b

Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variablevariables)

  • yash:

      array -i a 0 newvalue
    

Note that arrays in bash (copied from ksh) are rather associative arrays.

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that. Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variable)

  • yash:

      array -i a 0 newvalue
    

Note that arrays in bash (copied from ksh) are rather associative arrays.

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that.

If you wanted to insert the newvalue as ${a[0]} and shift all the other keys by one, you'd need a temporary array:

b=newvalue
for k in "${!a[@]}"; do
  b[k+1]=${a[k]}
done
unset a
for k in "${!b[@]}"; do
  a[k]=${b[k]}
done
unset b

Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variables)

  • yash:

      array -i a 0 newvalue
    
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

Note that arrays in bash (copied from ksh) are rather associative arrays.

a=(newvalue "$a[@]")

would make a new $a array with newvalue as ${a[0]} and the elements of the original array appended in the numerical order of their key with keys 1, 2...

For instance, if you had:

bash-4.4$ typeset -p a
declare -a a=([0]="foo" [12]="bar")
bash-4.4$ a=(newvalue "${a[@]}")
bash-4.4$ typeset -p a
declare -a a=([0]="newvalue" [1]="foo" [2]="bar")

That explains why there's no builtin operator for that. Shells like zsh or yash that have normal arrays have operators for that:

  • zsh:

      a[1,0]=newvalue
    

(also works for prepending strings to scalar variable)

  • yash:

      array -i a 0 newvalue