3

I am using fish shell and I can't seem to set the output of awk to a variable.

set installed_version (scala -version 2>&1 | awk 'NR==1{ print $5 }')

Any ideas why that's the case?

Edit: This works though

set foo (java -version 2>&1 | awk 'NR==1{ print $3 }')
aa8y
  • 3,854
  • 4
  • 37
  • 62
  • How are you determining that the variable is not being set? – ridiculous_fish Feb 23 '15 at 00:10
  • What's the output you get from running simply `scala -version` ? – jas Feb 23 '15 at 00:11
  • @ridiculous_fish By echoing the value of the variable. The output of `scala -version` is `Scala code runner version 2.11.5 -- Copyright 2002-2013, LAMP/EPFL`. And it's not just Scala. I have a similar command for SBT which is also not working. – aa8y Feb 23 '15 at 00:13
  • Try experimenting redirecting the output various ways: `scala -version >/dev/null`, `^/dev/null`, etc, to determine which stream it's really on. – glenn jackman Feb 23 '15 at 02:24

2 Answers2

4

scala is going into the background, starting a REPL because it thinks stdin is a terminal. This works for me:

set installed_version (scala -version 2>&1 < /dev/null | awk 'NR==1{ print $5 }')
echo $installed_version
Joe Hildebrand
  • 10,354
  • 2
  • 38
  • 48
1

This is fish bug #1949 - fish doesn't run command substitutions in a subprocess, and so leaves stdin connected to the tty. Because of that, some tools don't behave as the should.

Joe Hildebrand's workaround (explicitly redirect </dev/null) is the right thing to do currently.

faho
  • 14,470
  • 2
  • 37
  • 47