Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect echo output in shell script to logfile

Tags:

shell

stdout

I have a shell script with lots of echo in it. I would like to redirect the output to a logfile. I know there is the command call cmd > logfile.txt, or to do it in the file echo 'xy' > logfile.txt, but is it possible to simply set the filename in the script which then automatically writes all echo's to this file?

like image 541
wasp256 Avatar asked Sep 14 '14 13:09

wasp256


People also ask

How do I redirect echo output to a file in shell script?

$ echo “Hello” > hello. txt The > command redirects the standard output to a file. Here, “Hello” is entered as the standard input, and is then redirected to the file **… $ cat deserts.

How do I redirect a script to log file?

Normally, if you want to run a script and send its output to a logfile, you'd simply use Redirection: myscript >log 2>&1. Or to see the output on the screen and also redirect to a file: myscript 2>&1 | tee log (or better still, run your script within the script(1) command if your system has it).

How do I redirect the console output to a file in Linux?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do I copy a stdout to a file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!


1 Answers

You can add this line on top of your script:

#!/bin/bash # redirect stdout/stderr to a file exec >logfile.txt 2>&1 

OR else to redirect only stdout use:

exec > logfile.txt 
like image 193
anubhava Avatar answered Sep 17 '22 10:09

anubhava