Thursday, April 18, 2019

Backtick in bash

echo "scale = 4; .5/2" | bc -l
produces expected result: .2500

set x=`echo "scale = 4; .5/2" | bc -l`
executes but x is set to a blank.

set x=$(echo "scale = 4; .5/2" | bc -l)
does the same thing.  Has backtick changed since 2014?

Progress.  Bash variables need no spaces around the = and set is not required, or wanted.  The for loop:

for y in ($(seq -f "%0.4f" $y $advanceY $endY) does not increment by .1 (the value of advanceY), but by 1.  Duh: advanceY is not $advance.

This problem remains
echo "scale = 4; .5/2" | bc -l
produces expected result: .2500

x=`echo "scale = 4; .5/2" | bc -l`
executes but x is set to a blank.

x=$(echo "scale = 4; .5/2" | bc -l)
does the same thing.  Has backtick changed since 2014?




4 comments:

  1. The problem, I think, is "set". Just:

    x=`echo "scale = 4; .5/2" | bc -l`

    ReplyDelete
  2. set is a csh-ism.

    dropping it makes both of the next two lines work fine.

    ReplyDelete
  3. I can't duplicate your results on my machine (Fedora 30, runs bash 5.0.2).

    echo $SHELL

    [outputs /bin/bash]

    echo $BASH_VERSION

    [outputs 5.0.2(1)-release]

    x=`echo "scale = 4; .5/2" | bc -l`
    echo $x

    [outputs .2500]

    y=$(echo "scale = 4; .5/2" | bc -l)
    echo $y

    [also outputs .2500]

    ReplyDelete
  4. You use "set" in Windows and csh.

    Not in sh or bash.

    ReplyDelete