Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash script that I want to redirect all the rest of the output into a file.

# the part i dont know  ${file}
echo spo 
echo car

cat ${file}

output should be spo\ncar\n

share|improve this question
output redirection is made by adding ,> out 2>&1 at end of your execution command. – Amir Naghizadeh Nov 24 '12 at 20:07

If you redirect stdout for the rest of the script, then that would include the "cat" command, so you wouldn't see its output, $file could fill up indefinitely with recursive copies of itself (and would if it wouldn't be so small as to let cat see the end of it before starting to write to it).

To redirect stdout from now on, you simply do:

exec > "$file"

But here maybe you want:

#! /bin/bash -
  echo spo 
  echo car
} > "$file"

cat < "$file"

You can also do things like:

exec 3>&1 # save stdout as fd 3
exec > "$file"
echo spo
echo car
exec >&3 3>&- # restore stdout and close fd 3
cat < "$file"

(and by the way, the above is what modern shells do internally when you redirect a compound command like { some; code; } > ... (though they use a fd above 10 and set the O_CLOEXEC flag on it so that executed commands don't see it), while the Bourne shell would fork a subshell in that case)

Also note that in the code above, you don't have to restore stdout for the rest of the script, you can do it for the cat command only:

cat < "$file" >&3 3>&-

(the 3>&- (closing fd 3) is not necessary as cat doesn't use its fd 3 anyway but is good practice in the general case, and strictly speaking, we'd have to add it to every command to emulate the O_CLOEXEC behavior of the shell).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.