Find the answer to your Linux question:
Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By gregm
Hello, I would like to know if there is a way to export variables in bc to be read in bash in the terminal? An how to export variables from ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2012
    Location
    Pennsylvania
    Posts
    11

    bc calculator


    Hello, I would like to know if there is a way to export variables in bc to be read in bash in the terminal? An how to export variables from bash in the terminal to bc?
    Thank you for any help you can give.

  2. #2
    Linux Guru
    Join Date
    Dec 2013
    Location
    Victoria, B.C. Canada
    Posts
    1,650
    Code:
    #!/bin/bash
    let a=3
    let b=4
    
    bc <<EOS
    $a*$b
    EOS
    
    #or
    
    echo "$a*$b" | bc
    
    #both
    
    read c <<EOS
    $(echo "$a*$b" | bc)
    EOS
    
    echo "$c"
    amayamartin82 likes this.

  3. #3
    Just Joined!
    Join Date
    Mar 2012
    Location
    Pennsylvania
    Posts
    11
    gregm I want to thank you for your rapid reply to my post. Your answer does pass the value, 12=3*4, to the terminal, but does not pass the variable and it's value to bash. Like the export command can pass the variable=value to sh from bash. I am trying to get a bc variable=value into bash and then save it to a file with the redirection output, > .
    Last edited by heican; 03-10-2014 at 07:10 PM. Reason: improve meaning

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    gregm“s answer is correct and complete.
    Look at the variable $c.
    You must always face the curtain with a bow.

  6. #5
    Just Joined!
    Join Date
    Mar 2012
    Location
    Pennsylvania
    Posts
    11

    answer to guestion.

    Quote Originally Posted by Irithori View Post
    gregm“s answer is correct and complete.
    Look at the variable $c.
    lrithori Yes the value is echoed to my bash terminal. But at the prompt that follows when I echo $c I get a black $c variable. But with the help from you and gregm I learned that I can within the script file redirect echo $c output to a file. An this was what I was looking for. I thank you both for the help! An can not believe how fast my question was answered.

  7. #6
    Just Joined!
    Join Date
    Feb 2014
    Posts
    7
    Quote Originally Posted by heican View Post
    lrithori Yes the value is echoed to my bash terminal. But at the prompt that follows when I echo $c I get a black $c variable. But with the help from you and gregm I learned that I can within the script file redirect echo $c output to a file. An this was what I was looking for. I thank you both for the help! An can not believe how fast my question was answered.
    Even I was helped by that, as by reading the third post (the answer by gregm) left me with some doubt. Thanks guys for sharing the information.

  8. #7
    Just Joined!
    Join Date
    Oct 2014
    Posts
    1
    gregm I want to thank you for your rapid reply to my post. Your answer does pass the value, 12=3*4, to the terminal, but does not pass the variable and it's value to bash. Like the export command can pass the variable=value to sh from bash.




    _____________
    Nawaz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •