I have a string:


and a second one:


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.

  • 2
    Pipe it to od to see what it returns... Aug 29, 2018 at 19:28

4 Answers 4


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

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:

  • 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. Aug 29, 2018 at 19:43
  • Note that if the input has only one field, cut returns that field. Aug 29, 2018 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. Nov 14, 2021 at 17:41
  • @itzjackyscode Given unix.stackexchange.com/questions/65803/…, I should probably update my echo "$versionfull" part of the answer -- thank you for the nudge!
    – Jeff Schaller
    Nov 15, 2021 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, 2021 at 20:47

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
$ echo a.b | cut -d. -f3

$ echo a | cut -d. -f3

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:

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).

  • The probability of version 7 in Mikrotik is low ;) Anyway, thanks - I'll do that.
    – Kliwer
    Aug 29, 2018 at 20:11
  • How does this relate to his second case? The input has a delimiter.
    – Barmar
    Aug 29, 2018 at 22:59
  • 'returns the full line (so the first field) for those that don't have any delimiter' - wasn't aware of this behaviour. Thank you! :)
    – Technext
    Jan 24 at 10:05

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

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

  • Thanks. This is simple and works exactly like I want it to.
    – Kliwer
    Aug 29, 2018 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, 2018 at 9: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, 2018 at 11:43

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"

Without the quotes, the first line expands to:

if [ == "" ]

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.