What exactly does the following do?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Deletes the longest match of the substring
*\n from the start of the string stored in
MY_INPUT. Note that
* is a wildcard, so the result is that every line of
MY_INPUT is discarded except the last one, which is stored in
'\n' is put there just to evaluate
\n and consider it as a newline instead of literally the characters
MY_INPUT MY_VAR "a\nb\nc" -> "c" "a\n" -> "" "abcde\n\n\ndef" -> "def"
Note: in the second example the last line is empty so
MY_VAR is an empty string.
As requested, another way to do this could be:
tail -n1 <<< "$MY_INPUT"
echo "$MY_INPUT" | tail -n1