DIGIT-ARC read-only archive · snapshot: July 23, 2026

Bash Help Required

6 posts · originally from https://geek.digit.in/community/threads/bash-help-required.71366/

nileshgr
nileshgr Original post #1
Oct 23, 2007 at 7:04 PM
Hi,

I have a C++ program which simply reads and outputs a file. This output i need to be taken into a Bash variable. How do i do that ?

Or there is any other method to get a file output into a bash variable using bash itself instead of the C++ program ?

Its urgent!! :?:
QwertyManiac
Oct 23, 2007 at 7:16 PM
Code:
cat filename.ext |while read line; do echo "${line}"; done
As you see, cat filename.ext reads the file, and stores it in 'line' for each line encountered as the while progresses. You can replace the do method with whatever you wish, the trick is to load the file contents via cat.

This line prints whatever lines are found in file "filename.ext".
Sykora
Sykora #3
Oct 23, 2007 at 7:24 PM
In general,

Code:
$> variableName=$(command_to_execute_your_program)

That should do it. The only problem is, it won't work for programs in which you're going to enter input. ie, only the stuff printed to stdout using cout will be stored in the variable.
nileshgr
Oct 23, 2007 at 9:30 PM
@QM,

Thanks.

@Sykora,

you mean to say that if i set a variable name as a command to execute a program, then the program's output will go to the variable ?

Like:

Code:
output=$(program)
QwertyManiac
Oct 23, 2007 at 9:49 PM
Code:
output=$([B]./[/B]program-name)
echo $output
Sykora
Sykora #6
Oct 24, 2007 at 2:34 PM
The variable name should not be the command. You are merely telling bash to assign the output of tehe porogram to the variable using the above syntax.