I'm trying to replace some codes with bash function to reuse it. So my previous codes are:#!/bin/bashset aline= dsfsdf | fsdfff | yuhfd set line_parsed=`echo $aline | tr '|' \n`echo ${line_parsed[1]}Which outputs dsfsdf.Now, if I'm trying to put the sentence in a function, I cannot get it right:#!/bin/bashparseLine (){set line_parsed=`$echo $1 | tr '|' \n`echo ${line_parsed[1]}}set aline= dsfsdf | fsdfff | yuhfd parseLine $alineIs there a syntax error?
Parsing a string using bash function
bash;array
To create an array from string, use:IFS='|' read -r -a line_parsed <<< $alineIFS='|' sets bashs internal field seprator to |read reads from its standard input file descriptor-r Backslash does not act as an escape character.-a line_parsed create an array named line_parsed<<< $aline is a Here string, which passes the contents of $aline to reads standard input.You will now have an array. echo ${line_parsed[0]} will give you the first element of the splitted string.All of that in a function:parseLine (){  IFS='|' read -r -a line_parsed <<< $1  echo ${line_parsed[0]}}This can now be called like this:$ parseLine dsfsdf | fsdfff | yuhfd dsfsdf