Why does bc work with 'here string' and not via pipe in this example?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does bc work with 'here string' and not via pipe in this example?
# 1  
Old 01-22-2014
Question Why does bc work with 'here string' and not via pipe in this example?

Hi!

I actually got it running, but I still would like to understand, why and how, since I am a beginner in bash scripting. I Need floating numbers and thus use bc in my bash script. Here it is:

Code:
#!/bin/bash
num1="10^-15" | bc -l #power function piped to bc - DOES NOT WORK
echo $num1
fact="10^-15" | bc -l #power function piped to bc - DOES ONLY WORK WHEN TYPED IN TERMINAL
echo $fact
num2=$(bc -l <<< "10^-15") #'here string' to bc - WORKS
echo $num2

The result of Script:
Code:
# ./demo.sh 
 
 
.00000000000000100000

-> Only the last variable has the wanted value!


When I put the single commands in the terminal:
Code:
# num1="10^-15" | bc -l #power function piped to bc - DOES NOT WORK
# echo $num1
 
# fact="10^-15" | bc -l #power function piped to bc - DOES ONLY WORK WHEN TYPED IN TERMINAL
# echo $fact
10^-15
# num2=$(bc -l <<< "10^-15") #'here string' to bc - WORKS
# echo $num2
.00000000000000100000

-> Here the both last variables have the wanted value - in different formats, but I can do further calculations with them.

Why does the pipe fail? Why does it work in the terminal with the "fact" variable?

I am using:
Code:
# bash -help
GNU bash, version 4.1.2(1)-release-(x86_64-redhat-linux-gnu)



Thanks!
Martin
# 2  
Old 01-22-2014
What, exactly, does num1="10^-15" print to the screen when you type it in and hit enter?

Absolutely nothing at all.

What, exactly, does it feed into bc when you type it in and feed it into a pipe?

Absolutely nothing at all.

Pipes pick up the text a program prints. If you want to pipe text into bc, that text has to print to the screen.

Code:
echo "text" | bc

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-22-2014
The pipe doesn't fail - it just doesn't have anything to transmit. Try echoing $num1 and pipe that to bc.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-22-2014
You havent tried:
Code:
num1=$(echo "10^-15" | bc -l)

This User Gave Thanks to vbe For This Post:
# 5  
Old 01-22-2014
The reason that num1 remains unset in your first example is because the assignment occurs in a subshell.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace pipe delimited column string to null

Hi All, I have a large dat file where each lines are pipe delimited values. I need to parse the file depending on the request. For example: sometimes I have told to remove all the values in the 7th column (this case remove values '3333' only from the first line and '3543' from the second line)... (4 Replies)
Discussion started by: express14
4 Replies

2. Shell Programming and Scripting

Would pipe work better with this command

Hi again, have a script that I would like run, but before I can run it I need to strip out the windows \r end of lines. I have put the command into a text file and set the command to run every 10 seconds the coomand I use to do this is while sleep 10; do... (15 Replies)
Discussion started by: Paul Walker
15 Replies

3. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

4. Shell Programming and Scripting

how to build a pipe delimited string

#! /bin/csh set delimiter = | foreach i (*) set str_deli="$i$delimiter" question: how to retain the value of str_deli so i can build a pipe delimited string? end (1 Reply)
Discussion started by: jdsignature88
1 Replies

5. Shell Programming and Scripting

awk's getline < "-" seems not work for pipe

Hi all, I have an gawk script to get user's input, So I use getline name < "-" (or getline name < "/dev/stdin") in my script They both work fine when my script deals with files. But it is broken for pipes. When I try "some command | my awk script", the variable name just gets an empty... (17 Replies)
Discussion started by: qiulang
17 Replies

6. Shell Programming and Scripting

How to Avoid intermediate files when pipe does nt work

problem with piping one output to another.Would like to avoid the intermediate file creation.The piping does nt work on places where files have been created and goes in an endless loop. sed -e "s/^\.\///g" $LINE1| sed -e "s/_\(\)/kkk\1/g" > $file1 tr -s '_' ' ' < $file1| \ sort -n -k... (1 Reply)
Discussion started by: w020637
1 Replies

7. UNIX for Dummies Questions & Answers

How does pipe work?

I am confused over piping. :confused: A | B Will A and B run at the same time? or must A finish running before B starts to run? Suppose I want to do the following: sqlplus ... | split -1000 - filename_ sqlplus will return 1million rows, I want write the output into files of 1000... (4 Replies)
Discussion started by: Leion
4 Replies

8. UNIX for Dummies Questions & Answers

Pipe in command string

Hi, Can't you have a pipe in a command string ? If I try the following I get errors. Why ? > cmd="ls -lrt | grep xyz" > $cmd |: No such file or directory grep: No such file or directory xyx: No such file or directory Thanks in advance Hench (3 Replies)
Discussion started by: hench
3 Replies

9. Programming

output string message to pipe

i am new to linux programming. can anyone answer my question? there is one pipe file "my_pipe" prw-r--r-- 1 john rnd 32 Aug 17 19:45 my_pipe how to output string message (char*) to this pipe? which API should I use? (3 Replies)
Discussion started by: princelinux
3 Replies

10. Linux

By angle-brackets/"pipe" button doesn't work?

How can I configure it? I have a swedish keyboard with swedish keyboard setting. Everything works perfectly (едц) except that button. What can be wrong? /Richard ++ NOTE: It seems like the computer notices the input but that the button isn't assigned to anything (the keyboard-cursor stops).... (1 Reply)
Discussion started by: riwa
1 Replies
Login or Register to Ask a Question