Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect standard input and output with Bash

Tags:

bash

io

#!/bin/bash
./program < input.txt > output.txt

The > output.txt part is being ignored so output.txt ends up being empty.

This works for the sort command so I expected to also work for other programs.

Any reason this doesn't work? How should I achieve this?

like image 945
Inuart Avatar asked Apr 09 '12 16:04

Inuart


1 Answers

The most likely explanation is that the output you're seeing is from stderr, not stdout. To redirect both of them to a file, do this:

./program < input.txt > output.txt 2>&1

or

./program < input.txt &> output.txt
like image 100
Oliver Charlesworth Avatar answered Sep 20 '22 15:09

Oliver Charlesworth