Example:
before: text_before_specific_character(specific_character)text_to_be_deleted
after: text_before_specific_character
I know that
If TEXT contains your text, as in
TEXT=beforexafter
and the specific character happens (for example) to be x
, then
echo "${TEXT%x*}"
does what you want.
To Bash, "$TEXT"
or "${TEXT}"
is the whole beforexafter
, but "${TEXT%xafter}"
is just before
, with the xafter
chopped off the end. To chop off the x
and anything that might follow it, one writes "${TEXT%x*}"
.
There is also "${TEXT%%x*}"
, incidentally. This differs from the other only if there is more than one x
. With the %%
, Bash chops off all x
, whereas with the %
it chops off only from the last x
. You can remember this by observing loosely that the longer %%
chops off more text.
You can do likewise with Sed, of course, if you prefer:
echo "$TEXT" | sed 's/x.*//'