简体   繁体   中英

Store output of C program in shell

I have a C program which gives some output. I am compiling the C program via the shell but I need the output from the run C program and store in shell.

Edit.

Save the output to a shell Variable.

I assume that you want to store the output of the program in a variable. Unix shells offer a facility that's called command substitution to do just that. Depending on your shell, you can do either :

output=$(./run)

or

output=`./run`

Bash supports both. If, however, you want to save the output to a file, you will need to redirect the standard output stream to a file. You can do this like this :

./run > output.txt

Or, if you want to see the output while the program is running and save it to an output file as well, you can use the tee utility and pipe the output of your program to it.

./run | tee output.txt

You can redirect your output into a file like this:

./run > file

If you want to store it into a variable, you have to decide which shell we're talking about. It depends whether you have a windows shell, or linux bash..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM