NAME=PATH=USER=DATE
Multiple ways to extract this data. The easiest may be pattern filtering. Pattern filtering has four forms:
${VAR#PATTERN}
- Remove the smallest left most part of the string that matches the pattern.
${VAR##PATTERN}
- Remove the largest left most part of the string that matches.
${VAR%PATTERN}
- Remove the **smallest right most part of the string that matches.
${VAR%%PATTERN}
- Remove the largest right most part of the string that matches.
You can remember that #
is to the left of %
on the keyboard, so #
is left and %
is right.
STRING="NAME=PATH=USER=DATE"
PATH=${STRING#*=} # Removes NAME=
PATH=${PATH%%=*} # Removes =USER=DATE
echo $PATH # Echoes "PATH"
You might be able to use the read
to get all four at once. I am on an iPad, so I can't test this right now.
OLD_IFS="$IFS"
IFS="="
read NAME PATH USER DATE <<<"$STRING"
IFS="$OLDIFS"
$IFS
is thee Input File Separator and is set to space/tab/NL by default. I save the value of $IFS
before I change it. I set it to =
which separates your various values in your input string..
The read
will read in the values using $IFS
to separate each one. The <<<
is a way to get the shell variable in as input.
Once I finish getting the values, I reset IFS
. Otherwise, I would have problems later on.