How to extract last part of string in bash?

Simplest is

echo "$A" | awk '{print $NF}'

Edit: explanation of how this works…

awk breaks the input into different fields, using whitespace as the separator by default. Hardcoding 5 in place of NF prints out the 5th field in the input:

echo "$A" | awk '{print $5}'

NF is a built-in awk variable that gives the total number of fields in the current record. The following returns the number 5 because there are 5 fields in the string "Some variable has value abc.123":

echo "$A" | awk '{print NF}'

Combining $ with NF outputs the last field in the string, no matter how many fields your string contains.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)