What does cut return if the specified field does not exist

cuttext processing

I have a string:

6.40.4

and a second one:

6.40

I am using var=$(echo $versionfull | cut -d'.' -f3) to get the third digit from the first string in bash. What does this command return for the second one? It looks empty but either [ -z $var ] or [ $var == "" ] does not work. I want to give it a value of 0 in case of a second string.

Best Answer

You could use var=$(echo "${versionfull}.0" | cut -d'.' -f3).

In the first case, versionfull will contain 6.40.4.0, ignoring the padding and returning 4 as needed. In the second case, the .0 will be padded and returned.

Related Question