Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix shell replace string twice (in one line)

Tags:

bash

shell

unix

I run a script with the param -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 a single line?

Thanks in advance.

like image 220
Orkun Ozen Avatar asked Feb 15 '23 03:02

Orkun Ozen


1 Answers

One way

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

This places the output from the parameter substitution ${AIRLINE_OPTION//-A /} into a "here-string" and uses the bash read built-in to parse this into an array. Splitting by / is achieved by setting the value of IFS to / for the read command.

like image 109
iruvar Avatar answered Feb 24 '23 09:02

iruvar