lsblk - output not always sorted correctly

Hello.

On a fresh install computer :
Lang = Us
Keyboard layout = French
Desktop : KDE with initial configuration (no change unless show after)

user_install@ASUS-G731GV-JC:~> locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

ROOT_USES_LANG=yes

This output is correct :

user_install@ASUS-G731GV-JC:~> lsblk --noheadings -o name
sda
├─sda1
├─sda2
├─sda3
├─sda4
├─sda5
├─sda6
├─sda7
├─sda8
├─sda9
├─sda10
├─sda11
├─sda12
├─sda13
├─sda14
├─sda15
├─sda16
└─sda17
sdb
└─sdb1
nvme0n1
├─nvme0n1p1
├─nvme0n1p2
├─nvme0n1p3
├─nvme0n1p4
├─nvme0n1p5
├─nvme0n1p6
├─nvme0n1p7
├─nvme0n1p8
└─nvme0n1p9

This one is not :

user_install@ASUS-G731GV-JC:~> lsblk --noheadings -o kname
sda
sda1
sda2
sda3
sda4
sda5
sda6
sda7
sda8
sda9
sda10
sda11
sda12
sda13
sda14
sda15
sdb
sdb1
nvme0n1
nvme0n1p1
nvme0n1p2
nvme0n1p3
nvme0n1p4
nvme0n1p5
nvme0n1p6
nvme0n1p7
nvme0n1p8
nvme0n1p9
sda16
sda17

I use this output to call a script to print partition info.
This problem seems very new to me ( Not more than a quarter, I think. )

Any help is welcome.

You’ll have to be more specific how you created your output,
And what is the difference you’re concerned about between your correct and incorrect outputs… I see formatting and missing a couple partitions.

TSU

I do not see any partitions that are in one of the lists and missing in the other. Both have 30 entries.

Also I find that it is clear how the output is created. The commands are there to be seen.

To the OP.
I might be that what you see is not what you expected or wanted. That however does not mean it is “correct” or “wrong” as you call it.

I am not at all sure how the sequence is decided on. The first one has of course the dependency that partitions of a device are show after each device because of the “graphics”. I have no idea what decides the sequence of them. Is sda3 after sda2 because 3 comes after 2 in an ASCII sort, or because it is the next partition as defined in the partition tabel?
Also sda10 comes after sda9, which not an ASCII sort. this could point to partition label sequence and not alphabetical sequence.
In most cases partitions are in their numbered sequence in the partition table, but not always.

Also IMHO nv comes before sd in alphabetic, thus the main items in the first list are also not alphabetic.

You say it is correct, but I fail to see why. Any document you based that conclusion on?

In the second case I can not see any reason for why this is mixed up as it is, but that is of course also your concern.

As you mention you use this in a script, I want to cite (probably superfuous) from the man page:

The default output, as well as the default output from options like --fs and --topology, is subject to change. So whenever possible, you should avoid using default outputs in your scripts. Always explicitly define expected columns by using --output columns-list in environments where a stable output is required.

And while we are at the man page, there is no mentioning at all about a sequence of the devices or entries. Thus your claim to what is “correct” has no base. And isn’t your case solved by piping the output through sort?

lsblk enumerates devices reading /sys/block so devices are normally in directory order. It should collect partitions immediately before moving onto next device, so last two entries are certainly unexpected. Output of “ls -1U /sys/block” would be interesting.

Correct :
Command line : lsblk --noheadings -o name

user_install@ASUS-G731GV-JC:~> lsblk --noheadings -o name
sda
├─sda1
├─sda2
....
....
├─sda16
└─sda17
sdb
└─sdb1
nvme0n1
├─nvme0n1p1
├─nvme0n1p2

Not correct : Commande line : lsblk --noheadings -o kname

sda
sda1
sda2
.....
sda14
sda15
! ! ! !
**SDA16 and SDA17 ARE MISSING**
! ! ! !
sdb
sdb1
nvme0n1
nvme0n1p1
nvme0n1p2
nvme0n1p3
nvme0n1p4
nvme0n1p5
nvme0n1p6
nvme0n1p7
nvme0n1p8
nvme0n1p9
**sda16  ! ! ! !
sda17  ! ! ! !
**

I expect the output in lexical order ( that I mean correct from my point of view )
This is correct :

sda
├─sda1
├─sda2
....
....
├─sda16
└─sda17
sdb
└─sdb1
nvme0n1
├─nvme0n1p1
├─nvme0n1p2

This is not correct :

sda
sda1
sda2
.....
sda14
sda15
sdb
sdb1
nvme0n1
nvme0n1p1
nvme0n1p2
nvme0n1p3
nvme0n1p4
nvme0n1p5
nvme0n1p6
nvme0n1p7
nvme0n1p8
nvme0n1p9
sda16  ! ! ! !
sda17  ! ! ! !

Lexicographic order - Wikipedia
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters. This generalization consists primarily in defining a total order on the sequences (often called strings in computer science) of elements of a finite totally ordered set, often called an alphabet.

And i must add

This is not correct (from my point of view) :

sda
sda1
sda10
sda11
sda12
sda2
sda3

This is correct (from my point of view) :

sda
sda1
sda2
sda3
sda4
sda5
sda6
sda7
sda8
sda9
sda10
sda11
sda12

Well, when in your opinion

sda

comes before

nvme

in lexicographical order, I bow to your interpretation.

Again we differ in the interpretation of lexicographical order

And of course your claim that it should be in lexicographical order is solely based on what you think being “correct”. This thought has no base on anything in the man page.

But I am repeating myself, all of what I say in my last three posts is already in my earlier post. Just saying the same thing all over again is not very productive, so I stop with that.

The only thing I agree with you is that the second list is “strange”.

This as nothing to do with a man page but with :

Lexicographic order - Wikipedia
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters. This generalization consists primarily in defining a total order on the sequences (often called strings in computer science) of elements of a finite totally ordered set, often called an alphabet.