Skip to main content
Add link description. Grammar. Prompt is not relevant. Fix typo in filename.
Source Link

I met an interesting issue while working with this code from Stack Overflow: thistripleee's answer on "How to check if a file contains only zeros in a Linux shell?" code. Why

Why does the same bash code produce different result depends ofdepending on interactive shell or subshell? Make

Make all-zeros file with name 'yourfile'your_file.

a@L:~$$ truncate -s 1K your_file 

Interactive shell example

a@L:~$$ tr -d '\0' <your_file | grep -am 1 ^ ; echo $? 1 

The same code but using subshell

a@L:~$$ bash -c 'tr -d '\0' <your_file | grep -am 1 ^ ; echo $?' 0 

And also interesting fact. I changed original example by adding -a option ("equivalent to --binary-files=text") because without this option interactive shell works but subshell:

a@L:~$$ bash -c 'tr -d '\0' <your_file | grep -m 1 ^ ; echo $?' grep: (standard input): binary file matches 0 

P.S. I use bash 5.2.37(1)-release from Ubuntu 25.04

I met interesting issue while working with this code. Why the same bash code produce different result depends of interactive shell or subshell? Make all-zeros file with name 'yourfile'.

a@L:~$ truncate -s 1K your_file 

Interactive shell example

a@L:~$ tr -d '\0' <your_file | grep -am 1 ^ ; echo $? 1 

The same code but using subshell

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -am 1 ^ ; echo $?' 0 

And also interesting fact. I changed original example by adding -a option ("equivalent to --binary-files=text") because without this option interactive shell works but subshell:

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -m 1 ^ ; echo $?' grep: (standard input): binary file matches 0 

P.S. I use bash 5.2.37(1)-release from Ubuntu 25.04

I met an interesting issue while working with this code from Stack Overflow: tripleee's answer on "How to check if a file contains only zeros in a Linux shell?"

Why does the same bash code produce different result depending on interactive shell or subshell?

Make all-zeros file with name your_file.

$ truncate -s 1K your_file 

Interactive shell example

$ tr -d '\0' <your_file | grep -am 1 ^ ; echo $? 1 

The same code but using subshell

$ bash -c 'tr -d '\0' <your_file | grep -am 1 ^ ; echo $?' 0 

And also interesting fact. I changed original example by adding -a option ("equivalent to --binary-files=text") because without this option interactive shell works but subshell:

$ bash -c 'tr -d '\0' <your_file | grep -m 1 ^ ; echo $?' grep: (standard input): binary file matches 0 

P.S. I use bash 5.2.37(1)-release from Ubuntu 25.04

edited title
Link
muru
  • 78.3k
  • 16
  • 213
  • 319

grep behaviour is different in subshellwhen run using bash -c '...'

Became Hot Network Question
added 58 characters in body
Source Link

I met interesting issue while working with this code. Why the same bash code produce different result depends of interactive shell or subshell? Make all-zeros file with name 'yourfile'.

a@L:~$ truncate -s 1K your_file 

Interactive shell example

a@L:~$ tr -d '\0' <your_file | grep -am 1 ^ ; echo $? 1 

The same code but using subshell

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -am 1 ^ ; echo $?' 0 

And also interesting fact. I changed original example by adding -a option ("equivalent to --binary-files=text") because without this option interactive shell works but subshell:

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -m 1 ^ ; echo $?' grep: (standard input): binary file matches 0 

P.S. I use bash 5.2.37(1)-release from Ubuntu 25.04

I met interesting issue while working with this code. Why the same code produce different result depends of interactive shell or subshell? Make all-zeros file with name 'yourfile'.

a@L:~$ truncate -s 1K your_file 

Interactive shell example

a@L:~$ tr -d '\0' <your_file | grep -am 1 ^ ; echo $? 1 

The same code but using subshell

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -am 1 ^ ; echo $?' 0 

And also interesting fact. I changed original example by adding -a option ("equivalent to --binary-files=text") because without this option interactive shell works but subshell:

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -m 1 ^ ; echo $?' grep: (standard input): binary file matches 0 

I met interesting issue while working with this code. Why the same bash code produce different result depends of interactive shell or subshell? Make all-zeros file with name 'yourfile'.

a@L:~$ truncate -s 1K your_file 

Interactive shell example

a@L:~$ tr -d '\0' <your_file | grep -am 1 ^ ; echo $? 1 

The same code but using subshell

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -am 1 ^ ; echo $?' 0 

And also interesting fact. I changed original example by adding -a option ("equivalent to --binary-files=text") because without this option interactive shell works but subshell:

a@L:~$ bash -c 'tr -d '\0' <your_file | grep -m 1 ^ ; echo $?' grep: (standard input): binary file matches 0 

P.S. I use bash 5.2.37(1)-release from Ubuntu 25.04

added 1 character in body
Source Link
Arkadiusz Drabczyk
  • 26.6k
  • 5
  • 56
  • 70
Loading
Source Link
Loading