What I'd Like to do

I'd like to cut the output of the program xkb-switch -W, which infinitely waits for layout group change, and prints group names to stdout. I'm only really interested in the first two characters of the output, so id like to cut it with cut -c1-2.

What I'm trying to accomplish

What I'm really trying to accomplish here is showing my current keyboard layout in my i3 bar with i3blocks, but I don't want the length of the line changing, since xkb-switch outputs not only the layout language (the first two characters), but also the layout variant, which I'm not interested in seeing. At the moment, this is what my block looks like:

command=bash -c 'xkb-switch;xkb-switch -W'

What I've tried

xkb-switch -W > foo & while inotifywait -qe modify foo; do cut -c1-2 foo; done

I tried hacking together this oneliner, where the output of xkb-switch is redirected to a file, and by using inotifywait to wait for modifications to the file, cut is called to output the two characters I want to get.

This doesn't work however, because the file isn't clobbered by xkb-switch the way I'd initially intended (I understand why this wouldn't work), and also inotifywait outputs "foo MODIFY" when the file is modified, which I do not want. I guess the latter could be amended by redirection of some sort. But I suspect what I've tried is not the best approach.


Either I'm missing something important here or the simplest solution seems a bit too obvious: why not use a normal pipe?

xkb-switch -W | cut -c1-2

This will print the first two characters of each line xkb-switch outputs as soon as the line break is output.

  • That doesn't seem to work within the i3block configuration, although it does work just fine a terminal. So it seems I'm missing something how i3 or i3blocks works with the commands – Anton H Sep 15 '19 at 17:52

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.