@matteo-nunziati said in bash script help splitting string:
try this:
# initializing agi variables declare -a array while read -e ARG && [ "$ARG" ] ; do array=(` echo "$ARG" | sed -e 's/://'`) export ${array[0]}=${array[@]:1} #next added line for debug only. comment this out in prod. echo "${array[0]}=${array[@]:1}" doneif you want to "slice" an array use the syntax: ${array[@]:from:to}, not providing the 'to' arg means go up to the end of array.
source here.
Almost right. Because of the export command parsing spaces, I needed to stick it in a variable first.
# Variable to hold the details for the log file DUMPARG=" Begin Argument dump:\n" # Create an Array to hold the results of the loop declare -a array # Loop through the AGI variables while read -e ARG && [ "$ARG" ] ; do # Dump them into an array, after removing the : array=(` echo $ARG | sed -e 's/://'`) # take the array and create a variable from the first element put the rest as the value # value must be put into a holding variable to parse correctly by the export command val=${array[@]:1} export ${array[0]}="$val" # Dump them into a string for the log file DUMPARG="$DUMPARG $ARG\n" done