How to assign a variable to the output value of a program
so i wrote myself a very simple hellworld program in c++
...the usual stuff int main() { cout << "helloworld" << endl; return 0; } ------------------ how do i assign output value of this program to a variable in shell? Code:
myvar=$(./a.out); echo $myvar Code:
./a.out|myvar=$?; echo $myvar |
Your first attempt should work:
Code:
myvar=$(./a.out); echo $myvar EDIT: Also, what shell are you running? bash? ksh? tcsh? |
yes I do get "helloworld"
I am using bash --------------------- this is so confusing, i made myself the same program using python and it works...... but just not for c++... this doesnt make sense to me D: |
Well, if it works with your python program, then I'd say you've eliminated bash as a suspect. So it's likely something funky with the c++. For comparison, here's a quickie hello world source, compile, and run:
helloworld.cpp Code:
#include <iostream> Code:
$ g++ -o a.out helloworld.cpp |
All times are GMT -5. The time now is 09:39 PM. |