Search code examples
arrayslinuxbashappendprepend

Readarray with preppended or appended values in Bash


In Bash if I want to get a list of all available keyboard layouts but prepend my own keyboard layouts I can do:

readarray -t layouts < <(localectl list-x11-keymap-layouts)
layouts=("custom1" "custom2" "${kb_layouts[@]}")

If I want to append I can do:

readarray -t layouts < <(localectl list-x11-keymap-layouts)
layouts=("${kb_layouts[@]}" "custom1" "custom2")

Is it possible to achieve the same in a single line, in the readarray command?


Solution

  • Since the process substitution output <(..) is replaced by a FIFO for the processes to consume from, you can add more commands of choice inside. E.g. to append "custom1" "custom2" you just need to do

    readarray -t layouts < <(
      localectl list-x11-keymap-layouts; 
      printf '%s\n' "custom1" "custom2" )
    

    This creates one FIFO with contents from both the localectl output and printf output, so that readarray can read them as just another unique non-null lines. For prepend operation, have the same with printf output followed by localectl output.