9

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Kliwer
  • 93

4 Answers4

16

cut has an odd API.

cut -f n will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (so the first field) for those that don't have any delimiter:

$ echo a.b.c | cut -d. -f3
c
$ echo a.b | cut -d. -f3

$ echo a | cut -d. -f3 a

So the answer to What does cut return if the specified field does not exist? is either the first field or an empty field depending on whether the input line has one field or more.

You can add the -s option to remove the lines that don't have any delimiter to avoid that weird last case above, but that's generally not what you want (you'd generally want to consider that input line to have an empty 3rd field instead of skipping it altogether), and that's worse if you want the first field:

$ echo a | cut -sd. -f2
$ echo a | cut -sd. -f1
$

(you asked for the first field, the input has only one field, but you don't get any output because the input has no delimiter).

So @ThoriumBR's suggestion to add a .0 is a very good one. Without it:

versionfull=5
echo "$versionfull" | cut -d. -f3

would actually output 5. By adding .0, we make sure the input has at least one delimiter. I would go further and use:

echo "$versionfull.0.0" | cut -d. -f3

To make sure the input has at least 3 fields (here assuming $versionfull itself doesn't contain newline nor backslash characters, as cut cuts each line of its input; and \n is transformed to a newline character by many implementations of echo).

12

To answer your direct question, cut returns nothing except a trailing newline, as per the spec (thanks to don_crissti for that reference):

echo "$versionfull" | cut -d. -f3 | od -c
0000000  \n
0000001

If you have a shell that supports here-strings, you could do the following:

IFS=. read a b c <<< "$versionfull"

Notice the quoting for $versionfull, in case it ever had whitespace (anything from $IFS).

If you think that c might be empty, then ask to set it to zero:

c=${c:-0}
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 2
    Note that the need to quote in IFS=. read a b c <<< "$versionfull" was only for older versions of bash; other shells and newer versions of bash don't have the problem. – Stéphane Chazelas Aug 29 '18 at 19:43
  • Note that if the input has only one field, cut returns that field. – Stéphane Chazelas Aug 29 '18 at 20:05
  • also, for the here-string support, that can be replaced by echo -n "$versionfull" | read a b c if that isn't an option. – itzjackyscode Nov 14 '21 at 17:41
  • @itzjackyscode Given https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo, I should probably update my echo "$versionfull" part of the answer -- thank you for the nudge! – Jeff Schaller Nov 15 '21 at 20:46
  • Just realized that the echo wasn't guidance, but copying the Question, in order to demonstrate the behavior of cut. I'll leave it alone for now. – Jeff Schaller Nov 15 '21 at 20:47
10

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.

ThoriumBR
  • 215
  • Thanks. This is simple and works exactly like I want it to. – Kliwer Aug 29 '18 at 19:39
  • 3
    It's sad that this is marked as the solution because it doesn't even answer the question, which is what everyone will expect to find when they come here in the future with the same question. Maybe the question should be changed to reflect that OP doesn't actually care about what it returns. – pipe Aug 30 '18 at 09:28
  • You are right, it does not answers the question, but it solves OP problem. Is tailored for this only case, but that's all OP wants - solve his problem. – ThoriumBR Aug 30 '18 at 11:43
2

It sets it to an empty string. You should quote the variable when using it with the [ command.

if [ "$var" == "" ]
then echo "Var is empty"
fi

Without the quotes, the first line expands to:

if [ == "" ]

which is invalid syntax because == requires a parameter to the left of it.

Barmar
  • 9,927