Unix shell replaces a string twice (on a single line)

I am running a script with a parameter -A AA/BB. To get an array with AA and BB, I can do this.

INPUT_PARAM=(${AIRLINE_OPTION//-A / }) #get rid of the '-A ' in the begining
LIST=(${AIRLINES_PARAM//\// })         # split by '/'

Can we achieve this in one line?

Thanks in advance.

+4
source share
3 answers

One of the methods

IFS=/ read -r -a LIST <<< "${AIRLINE_OPTION//-A /}"

This puts the output from parameter substitution ${AIRLINE_OPTION//-A /}in the "here-string" string and uses the built-in bash readto parse this array. Separation on /is achieved by setting the values IFSin /for the team read.

+4
source

awk, , LIST:

$ LIST=($(awk -F"[\/ ]" '{print $2,$3}' <<< "-A AA/BB"))

:

$ echo ${LIST[0]}
AA
$ echo ${LIST[1]}
BB

  • -F"[\/ ]" : /.
  • '{print $2$3}' 2- 3- .
+2
LIST=( $(IFS=/; for x in ${AIRLINE_OPTION#-A }; do printf "$x "; done) )

This is a portable solution, but if yours readsupports -a, and you are not against portability, then you should go for the @ 1_CR solution.

+2
source

All Articles