You are not logged in.

#1 2025-10-14 11:40:31

craig_mc
Member
Registered: 2018-09-08
Posts: 6

[SOLVED] Odd Bash Behaviour

Hi.

Can anyone explain why below outputs words longer than 6 characters by inserting the letter m into words such as zigzmamg (zigzag) and zodimamc (zodiac)? If appears to place an m before and after the character used as the argument for the first grep. Tried on Debian but the output there is as would be expected (as it is on Arch using Z shell). Also, it only seems to happen when the letter m is used as the argument for the second grep. Is this a known bug?

awk 'length($0) == 6' /usr/share/dict/words | grep a | grep m

Thanks.

Last edited by craig_mc (2025-10-14 12:55:37)

Offline

#2 2025-10-14 12:11:06

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,417
Website

Re: [SOLVED] Odd Bash Behaviour

I don't encounter this issue:

[neumann@PDLap275W11 ~]$ cat words
zigzag
zodiac
[neumann@PDLap275W11 ~]$ awk 'length($0) == 6' words | grep a | grep m
[neumann@PDLap275W11 ~]$ bash --version
GNU bash, Version 5.3.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2025 Free Software Foundation, Inc.
Lizenz GPLv3+: GNU GPL Version 3 oder jünger <http://gnu.org/licenses/gpl.html>

Dies ist freie Software. Sie darf verändert und weitergegeben werden.
Es wird keinerlei Garantie gewährt, soweit es das Gesetz zulässt.
[neumann@PDLap275W11 ~]$ 

And where did you get /usr/share/dict/words from?

Sanity check

$ type grep
$ which grep

Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3 2025-10-14 12:36:22

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,058

Re: [SOLVED] Odd Bash Behaviour

Also

shopt

though even for lastpipe that's wild - more likely some grep alias…

Online

#4 2025-10-14 12:54:01

craig_mc
Member
Registered: 2018-09-08
Posts: 6

Re: [SOLVED] Odd Bash Behaviour

Thanks all.

It was caused by this.

alias grep='grep --color=always'

I can get back to cheating at word games now.

Offline

#5 2025-10-14 13:18:17

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,058

Re: [SOLVED] Odd Bash Behaviour

"--color=auto", do you understand what happened?
Edit: tbh, I can - but had not anticipated this happening.

Last edited by seth (2025-10-14 13:18:48)

Online

#6 2025-10-14 13:21:03

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,417
Website

Re: [SOLVED] Odd Bash Behaviour

Ah yes. grepping the ANSI color codes when you force grep to also throw them into the pipe, not just into an interactive TTY.

Edit: This is also fun:

[neumann@PDLap275W11 ~]$ alias grep='grep --color=always'
[neumann@PDLap275W11 ~]$ awk 'length($0) == 6' words | grep a | /bin/grep "\["
zigzag
zodiac
[neumann@PDLap275W11 ~]$ 

zigzag
zodiac

Last edited by schard (2025-10-14 13:25:10)


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#7 2025-10-14 13:23:03

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,058

Re: [SOLVED] Odd Bash Behaviour

Meh, I wanted to the OP to think about that a bit sad

Online

#8 2025-10-14 23:28:46

Succulent of your garden
Member
From: Majestic kingdom of pot plants
Registered: 2024-02-29
Posts: 855

Re: [SOLVED] Odd Bash Behaviour

seth wrote:

Meh, I wanted to the OP to think about that a bit sad

What is happening under the hood ? I mean I can't see any /n to notice some end of new line, it does seems that also  /m is not a thing but maybe [m it does for ansi code. But not sure how it is linked to how grep works with string manipulations under the hood :C


str( @soyg ) == str( @potplant ) btw!

Offline

#9 Yesterday 05:13:10

256
Member
Registered: 2023-12-17
Posts: 47

Re: [SOLVED] Odd Bash Behaviour

Succulent of your garden wrote:

What is happening under the hood ? I mean I can't see any /n to notice some end of new line, it does seems that also  /m is not a thing but maybe [m it does for ansi code. But not sure how it is linked to how grep works with string manipulations under the hood :C

This is indeed an ANSI terminal code issue. grep highlights the match in red using some ANSI commands, including "\e[01;31m" and "\e[m":

$ echo $'abc\ndef' | grep --color=always c | od -t a
0000000   a   b esc   [   0   1   ;   3   1   m esc   [   K   c esc   [
0000020   m esc   [   K  nl
0000025

This process is then repeated with the 'm' in those commands being highlighted:

$ echo $'abc\ndef' | grep --color=always c | grep --color=always m | od -t a
0000000   a   b esc   [   0   1   ;   3   1 esc   [   0   1   ;   3   1
0000020   m esc   [   K   m esc   [   m esc   [   K esc   [   K   c esc
0000040   [ esc   [   0   1   ;   3   1   m esc   [   K   m esc   [   m
0000060 esc   [   K esc   [   K  nl
0000067

You can get similar results grepping for K (at least on my system), as well as some of the other characters that appear. If you keep grepping over and over again the output grows exponentially.

The moral of the story is that stdout is not always a terminal.

Last edited by 256 (Yesterday 05:13:57)


"Don't comment bad code - rewrite it." - The Elements of Programming Style (1978), Brian W. Kernighan & P. J. Plauger, p. 144.

Offline

Board footer

Powered by FluxBB