How do I store the output of a command in a variable? Here is what I am doing but nothing is being stored. It seems that pipes are the issue, but I don't know how to proceed debugging.

This works as expected:

$ var=$(printf "hello \n world")
$ echo $var
hello world

As does this:

$ printf "hello \n world" | wc -l

This does not:

$ var=$(printf "hello \n world" | wc -l)
$ echo $var

$ #previous command returns nothing

How do I get var to store 1 in the third example?

Edit: Using bash, on Cygwin.

Edit 2: Running with bash -x

$ bash -x
+ [[ -z '' ]]
+ [[ himxBH != *i* ]]
+ export 'EXECIGNORE=*.dll'
+ EXECIGNORE='*.dll'
+ PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
+ [[ himxBH != *i* ]]

$ var=$(printf "hello \n world" | wc -l)
+ var=

$ echo $var
+ echo

Edit 3:

$ bash --version
GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Edit 4: set +o posix as suggested by @GeorgeVasiliou in a comment on my answer (now deleted by moderator, and copied below) does not change the output.

For some reason, the backtick syntax works for me, but not the $(...) syntax

$ var=`printf "hello \n world" | wc -l`
++ printf 'hello \n world'
++ wc -l
+ var=1

$ echo $var
+ echo 1

If someone can explain why this happens, and more importantly how to fix it, that one should be the accepted answer.

  • Which shell is this? May 12, 2017 at 22:55
  • 2
    Run bash -x, repeat the exercise and paste whole output here.
    – jimmij
    May 12, 2017 at 23:05
  • 1
    That is fascinating - the contents of the command substitution aren't even being run. It looks like the subshell is silently failing to start. May 12, 2017 at 23:15
  • 1
    What bash is this (bash --version)?
    – jimmij
    May 12, 2017 at 23:28
  • 1
    If $(…) and backticks behave differently (outside of corner cases involving parsing what's inside, and your code is not a corner case), it would be a bug. A rather strange one. May 13, 2017 at 22:22

1 Answer 1


[edited to add the links summary as suggested] It has to do with the fact that you're using Cygwin with certain software that causes it to behave strangely on Windows. The list of problematic software is called BLODA. Check the links below for more info:

The first link is the official Cygwin definition of BLODA. Software, usually antivirus type, that implements various hooks such as when a process executes fork(), or accesses files or even memory areas. This kind of software prevents Cygwin and bash to behave correctly and the solution would be either disable the scanning by adding an exception for Cygwin's installation directory or disabling/uninstalling the software altogether.

The second link points to a mailing list problem that manifests exactly as the author of this post describes it and someone answers that question pointing to the BLODA section of the Cygwin FAQ.

  • 1
    Thanks @Fox, I included a more detailed explanation.
    – Alin Dobre
    Feb 26, 2018 at 4:55

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.