What does “exec 1>/var/opt/log/my_logs/MYPROG_`date '+%Y%m%d_%H%M%S'`.log 2>&1” do?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite












I'm trying to understand what the below piece of shell script is doing. I know that exec without any arguments redirect the output of the current shell but am not able to understand what the below command does:



exec 1>/var/opt/log/my_logs/MYPROG_`date '+%Y%m%d_%H%M%S'`.log 2>&1






share|improve this question






















  • Those ` s are most probably not supposed to be there. :) You didn't need to escape the ``s to keep them from being interpreted as inline code because you intented the text into a code block.... Seems like I'm one to talk...
    – JoL
    Apr 23 at 17:48











  • It's just the difficulties of the stackexchange formatting. But it's clear enough for practical purposes
    – Sergiy Kolodyazhnyy
    Apr 23 at 18:19










  • @JoL That's my mistake. When I edited the post, I changed the formatting in a way that caused the text to be shown literally, but I failed to remove the backslashes that had previously served to escape the backticks. Sorry about that and thanks for pointing it out -- I've fixed it.
    – Eliah Kagan
    Apr 23 at 21:10










  • This question needs a better title.
    – Dennis Williamson
    Apr 23 at 22:06














up vote
2
down vote

favorite












I'm trying to understand what the below piece of shell script is doing. I know that exec without any arguments redirect the output of the current shell but am not able to understand what the below command does:



exec 1>/var/opt/log/my_logs/MYPROG_`date '+%Y%m%d_%H%M%S'`.log 2>&1






share|improve this question






















  • Those ` s are most probably not supposed to be there. :) You didn't need to escape the ``s to keep them from being interpreted as inline code because you intented the text into a code block.... Seems like I'm one to talk...
    – JoL
    Apr 23 at 17:48











  • It's just the difficulties of the stackexchange formatting. But it's clear enough for practical purposes
    – Sergiy Kolodyazhnyy
    Apr 23 at 18:19










  • @JoL That's my mistake. When I edited the post, I changed the formatting in a way that caused the text to be shown literally, but I failed to remove the backslashes that had previously served to escape the backticks. Sorry about that and thanks for pointing it out -- I've fixed it.
    – Eliah Kagan
    Apr 23 at 21:10










  • This question needs a better title.
    – Dennis Williamson
    Apr 23 at 22:06












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to understand what the below piece of shell script is doing. I know that exec without any arguments redirect the output of the current shell but am not able to understand what the below command does:



exec 1>/var/opt/log/my_logs/MYPROG_`date '+%Y%m%d_%H%M%S'`.log 2>&1






share|improve this question














I'm trying to understand what the below piece of shell script is doing. I know that exec without any arguments redirect the output of the current shell but am not able to understand what the below command does:



exec 1>/var/opt/log/my_logs/MYPROG_`date '+%Y%m%d_%H%M%S'`.log 2>&1








share|improve this question













share|improve this question




share|improve this question








edited Apr 23 at 21:08









Eliah Kagan

79.4k20221359




79.4k20221359










asked Apr 23 at 16:00









IA39

141




141











  • Those ` s are most probably not supposed to be there. :) You didn't need to escape the ``s to keep them from being interpreted as inline code because you intented the text into a code block.... Seems like I'm one to talk...
    – JoL
    Apr 23 at 17:48











  • It's just the difficulties of the stackexchange formatting. But it's clear enough for practical purposes
    – Sergiy Kolodyazhnyy
    Apr 23 at 18:19










  • @JoL That's my mistake. When I edited the post, I changed the formatting in a way that caused the text to be shown literally, but I failed to remove the backslashes that had previously served to escape the backticks. Sorry about that and thanks for pointing it out -- I've fixed it.
    – Eliah Kagan
    Apr 23 at 21:10










  • This question needs a better title.
    – Dennis Williamson
    Apr 23 at 22:06
















  • Those ` s are most probably not supposed to be there. :) You didn't need to escape the ``s to keep them from being interpreted as inline code because you intented the text into a code block.... Seems like I'm one to talk...
    – JoL
    Apr 23 at 17:48











  • It's just the difficulties of the stackexchange formatting. But it's clear enough for practical purposes
    – Sergiy Kolodyazhnyy
    Apr 23 at 18:19










  • @JoL That's my mistake. When I edited the post, I changed the formatting in a way that caused the text to be shown literally, but I failed to remove the backslashes that had previously served to escape the backticks. Sorry about that and thanks for pointing it out -- I've fixed it.
    – Eliah Kagan
    Apr 23 at 21:10










  • This question needs a better title.
    – Dennis Williamson
    Apr 23 at 22:06















Those ` s are most probably not supposed to be there. :) You didn't need to escape the ``s to keep them from being interpreted as inline code because you intented the text into a code block.... Seems like I'm one to talk...
– JoL
Apr 23 at 17:48





Those ` s are most probably not supposed to be there. :) You didn't need to escape the ``s to keep them from being interpreted as inline code because you intented the text into a code block.... Seems like I'm one to talk...
– JoL
Apr 23 at 17:48













It's just the difficulties of the stackexchange formatting. But it's clear enough for practical purposes
– Sergiy Kolodyazhnyy
Apr 23 at 18:19




It's just the difficulties of the stackexchange formatting. But it's clear enough for practical purposes
– Sergiy Kolodyazhnyy
Apr 23 at 18:19












@JoL That's my mistake. When I edited the post, I changed the formatting in a way that caused the text to be shown literally, but I failed to remove the backslashes that had previously served to escape the backticks. Sorry about that and thanks for pointing it out -- I've fixed it.
– Eliah Kagan
Apr 23 at 21:10




@JoL That's my mistake. When I edited the post, I changed the formatting in a way that caused the text to be shown literally, but I failed to remove the backslashes that had previously served to escape the backticks. Sorry about that and thanks for pointing it out -- I've fixed it.
– Eliah Kagan
Apr 23 at 21:10












This question needs a better title.
– Dennis Williamson
Apr 23 at 22:06




This question needs a better title.
– Dennis Williamson
Apr 23 at 22:06










1 Answer
1






active

oldest

votes

















up vote
7
down vote













There's actually 4 important bits of information that are happening:



  1. The exec built-in is used to redirect all output for command-line session to a file


  2. The 1><FILENAME> tells the shell to redirect stdout standard stream, i.e. normal non-error output of commands will go to <FILENAME>. The > will create if <FILENAME> doesn't exist or truncate if <FILENAME> already exists.


  3. The redirected filename is created via added backticks with date '+%Y%m%d_%H%M%S' command. Backticks are form of Command Substitution and are functionally equivalent to $(date '+%Y%m%d_%H%M%S') form, and nowadays $(...) is preferred for readability and because this form can be easily nested (i.e., have multiple levels). So output of date with the specified format '+%Y%m%d_%H%M%S' will create filename that is timestamped. If your command ran on 2018, July, 4th, 4:20:20 the output will be /var/opt/log/my_logs/MYPROG_20180704_042020.log.


  4. 2>&1 redirects stderr stream to that file also, it's a standard, POSIX compliant (which means Bourne-like shells other than bash understand it)form. This is functionally equivalent to &> bash-specific syntax. The order of specified redirections in shell is important, hence why this appears after 1> redirection.


In conclusion, there should be no output from this command itself. It should only rewire two output streams of all successive commands to go into the file you specified.



Interestingly enough, with this command my bash 4.4 outputs everything to file, and that includes the prompt and anything I type (so here I had to type blindly echo hello world and hit Ctrl+D to exit afterwards):



$ bash --posix
bash-4.4$ exec 1>./mylog_`date '+%Y%m%d_%H%M%S'`.log 2>&1
$ cat ./mylog_20180424_010800.log
bash-4.4$ echo hello world
hello world
bash-4.4$ exit


Doing that piece by piece, reveals that bash outputs prompt to stderr stream and surprisingly along with anything I type:



bash-4.4$ exec 1> ./mylog.txt
bash-4.4$ echo Hello World
bash-4.4$ cat ./mylog.txt
cat: ./mylog.txt: input file is output file
bash-4.4$ exec 2>&1


In case of ksh same thing happens, but I can see what is being typed, only the prompt goes to file, i.e. stdin isn't redirected:



bash-4.4$ ksh
$ exec 1>./mylog 2>&1
echo hello askubuntu
bash-4.4$ cat ./mylog
$ hello askubuntu
$
bash-4.4$


So here we can kinda see that shells can choose to output the prompt PS1 also to one of the standard streams, so that gets included into a file.






share|improve this answer


















  • 1




    Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
    – glenn jackman
    Apr 23 at 17:32










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1027510%2fwhat-does-exec-1-var-opt-log-my-logs-myprog-date-ymd-hms-log-21%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote













There's actually 4 important bits of information that are happening:



  1. The exec built-in is used to redirect all output for command-line session to a file


  2. The 1><FILENAME> tells the shell to redirect stdout standard stream, i.e. normal non-error output of commands will go to <FILENAME>. The > will create if <FILENAME> doesn't exist or truncate if <FILENAME> already exists.


  3. The redirected filename is created via added backticks with date '+%Y%m%d_%H%M%S' command. Backticks are form of Command Substitution and are functionally equivalent to $(date '+%Y%m%d_%H%M%S') form, and nowadays $(...) is preferred for readability and because this form can be easily nested (i.e., have multiple levels). So output of date with the specified format '+%Y%m%d_%H%M%S' will create filename that is timestamped. If your command ran on 2018, July, 4th, 4:20:20 the output will be /var/opt/log/my_logs/MYPROG_20180704_042020.log.


  4. 2>&1 redirects stderr stream to that file also, it's a standard, POSIX compliant (which means Bourne-like shells other than bash understand it)form. This is functionally equivalent to &> bash-specific syntax. The order of specified redirections in shell is important, hence why this appears after 1> redirection.


In conclusion, there should be no output from this command itself. It should only rewire two output streams of all successive commands to go into the file you specified.



Interestingly enough, with this command my bash 4.4 outputs everything to file, and that includes the prompt and anything I type (so here I had to type blindly echo hello world and hit Ctrl+D to exit afterwards):



$ bash --posix
bash-4.4$ exec 1>./mylog_`date '+%Y%m%d_%H%M%S'`.log 2>&1
$ cat ./mylog_20180424_010800.log
bash-4.4$ echo hello world
hello world
bash-4.4$ exit


Doing that piece by piece, reveals that bash outputs prompt to stderr stream and surprisingly along with anything I type:



bash-4.4$ exec 1> ./mylog.txt
bash-4.4$ echo Hello World
bash-4.4$ cat ./mylog.txt
cat: ./mylog.txt: input file is output file
bash-4.4$ exec 2>&1


In case of ksh same thing happens, but I can see what is being typed, only the prompt goes to file, i.e. stdin isn't redirected:



bash-4.4$ ksh
$ exec 1>./mylog 2>&1
echo hello askubuntu
bash-4.4$ cat ./mylog
$ hello askubuntu
$
bash-4.4$


So here we can kinda see that shells can choose to output the prompt PS1 also to one of the standard streams, so that gets included into a file.






share|improve this answer


















  • 1




    Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
    – glenn jackman
    Apr 23 at 17:32














up vote
7
down vote













There's actually 4 important bits of information that are happening:



  1. The exec built-in is used to redirect all output for command-line session to a file


  2. The 1><FILENAME> tells the shell to redirect stdout standard stream, i.e. normal non-error output of commands will go to <FILENAME>. The > will create if <FILENAME> doesn't exist or truncate if <FILENAME> already exists.


  3. The redirected filename is created via added backticks with date '+%Y%m%d_%H%M%S' command. Backticks are form of Command Substitution and are functionally equivalent to $(date '+%Y%m%d_%H%M%S') form, and nowadays $(...) is preferred for readability and because this form can be easily nested (i.e., have multiple levels). So output of date with the specified format '+%Y%m%d_%H%M%S' will create filename that is timestamped. If your command ran on 2018, July, 4th, 4:20:20 the output will be /var/opt/log/my_logs/MYPROG_20180704_042020.log.


  4. 2>&1 redirects stderr stream to that file also, it's a standard, POSIX compliant (which means Bourne-like shells other than bash understand it)form. This is functionally equivalent to &> bash-specific syntax. The order of specified redirections in shell is important, hence why this appears after 1> redirection.


In conclusion, there should be no output from this command itself. It should only rewire two output streams of all successive commands to go into the file you specified.



Interestingly enough, with this command my bash 4.4 outputs everything to file, and that includes the prompt and anything I type (so here I had to type blindly echo hello world and hit Ctrl+D to exit afterwards):



$ bash --posix
bash-4.4$ exec 1>./mylog_`date '+%Y%m%d_%H%M%S'`.log 2>&1
$ cat ./mylog_20180424_010800.log
bash-4.4$ echo hello world
hello world
bash-4.4$ exit


Doing that piece by piece, reveals that bash outputs prompt to stderr stream and surprisingly along with anything I type:



bash-4.4$ exec 1> ./mylog.txt
bash-4.4$ echo Hello World
bash-4.4$ cat ./mylog.txt
cat: ./mylog.txt: input file is output file
bash-4.4$ exec 2>&1


In case of ksh same thing happens, but I can see what is being typed, only the prompt goes to file, i.e. stdin isn't redirected:



bash-4.4$ ksh
$ exec 1>./mylog 2>&1
echo hello askubuntu
bash-4.4$ cat ./mylog
$ hello askubuntu
$
bash-4.4$


So here we can kinda see that shells can choose to output the prompt PS1 also to one of the standard streams, so that gets included into a file.






share|improve this answer


















  • 1




    Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
    – glenn jackman
    Apr 23 at 17:32












up vote
7
down vote










up vote
7
down vote









There's actually 4 important bits of information that are happening:



  1. The exec built-in is used to redirect all output for command-line session to a file


  2. The 1><FILENAME> tells the shell to redirect stdout standard stream, i.e. normal non-error output of commands will go to <FILENAME>. The > will create if <FILENAME> doesn't exist or truncate if <FILENAME> already exists.


  3. The redirected filename is created via added backticks with date '+%Y%m%d_%H%M%S' command. Backticks are form of Command Substitution and are functionally equivalent to $(date '+%Y%m%d_%H%M%S') form, and nowadays $(...) is preferred for readability and because this form can be easily nested (i.e., have multiple levels). So output of date with the specified format '+%Y%m%d_%H%M%S' will create filename that is timestamped. If your command ran on 2018, July, 4th, 4:20:20 the output will be /var/opt/log/my_logs/MYPROG_20180704_042020.log.


  4. 2>&1 redirects stderr stream to that file also, it's a standard, POSIX compliant (which means Bourne-like shells other than bash understand it)form. This is functionally equivalent to &> bash-specific syntax. The order of specified redirections in shell is important, hence why this appears after 1> redirection.


In conclusion, there should be no output from this command itself. It should only rewire two output streams of all successive commands to go into the file you specified.



Interestingly enough, with this command my bash 4.4 outputs everything to file, and that includes the prompt and anything I type (so here I had to type blindly echo hello world and hit Ctrl+D to exit afterwards):



$ bash --posix
bash-4.4$ exec 1>./mylog_`date '+%Y%m%d_%H%M%S'`.log 2>&1
$ cat ./mylog_20180424_010800.log
bash-4.4$ echo hello world
hello world
bash-4.4$ exit


Doing that piece by piece, reveals that bash outputs prompt to stderr stream and surprisingly along with anything I type:



bash-4.4$ exec 1> ./mylog.txt
bash-4.4$ echo Hello World
bash-4.4$ cat ./mylog.txt
cat: ./mylog.txt: input file is output file
bash-4.4$ exec 2>&1


In case of ksh same thing happens, but I can see what is being typed, only the prompt goes to file, i.e. stdin isn't redirected:



bash-4.4$ ksh
$ exec 1>./mylog 2>&1
echo hello askubuntu
bash-4.4$ cat ./mylog
$ hello askubuntu
$
bash-4.4$


So here we can kinda see that shells can choose to output the prompt PS1 also to one of the standard streams, so that gets included into a file.






share|improve this answer














There's actually 4 important bits of information that are happening:



  1. The exec built-in is used to redirect all output for command-line session to a file


  2. The 1><FILENAME> tells the shell to redirect stdout standard stream, i.e. normal non-error output of commands will go to <FILENAME>. The > will create if <FILENAME> doesn't exist or truncate if <FILENAME> already exists.


  3. The redirected filename is created via added backticks with date '+%Y%m%d_%H%M%S' command. Backticks are form of Command Substitution and are functionally equivalent to $(date '+%Y%m%d_%H%M%S') form, and nowadays $(...) is preferred for readability and because this form can be easily nested (i.e., have multiple levels). So output of date with the specified format '+%Y%m%d_%H%M%S' will create filename that is timestamped. If your command ran on 2018, July, 4th, 4:20:20 the output will be /var/opt/log/my_logs/MYPROG_20180704_042020.log.


  4. 2>&1 redirects stderr stream to that file also, it's a standard, POSIX compliant (which means Bourne-like shells other than bash understand it)form. This is functionally equivalent to &> bash-specific syntax. The order of specified redirections in shell is important, hence why this appears after 1> redirection.


In conclusion, there should be no output from this command itself. It should only rewire two output streams of all successive commands to go into the file you specified.



Interestingly enough, with this command my bash 4.4 outputs everything to file, and that includes the prompt and anything I type (so here I had to type blindly echo hello world and hit Ctrl+D to exit afterwards):



$ bash --posix
bash-4.4$ exec 1>./mylog_`date '+%Y%m%d_%H%M%S'`.log 2>&1
$ cat ./mylog_20180424_010800.log
bash-4.4$ echo hello world
hello world
bash-4.4$ exit


Doing that piece by piece, reveals that bash outputs prompt to stderr stream and surprisingly along with anything I type:



bash-4.4$ exec 1> ./mylog.txt
bash-4.4$ echo Hello World
bash-4.4$ cat ./mylog.txt
cat: ./mylog.txt: input file is output file
bash-4.4$ exec 2>&1


In case of ksh same thing happens, but I can see what is being typed, only the prompt goes to file, i.e. stdin isn't redirected:



bash-4.4$ ksh
$ exec 1>./mylog 2>&1
echo hello askubuntu
bash-4.4$ cat ./mylog
$ hello askubuntu
$
bash-4.4$


So here we can kinda see that shells can choose to output the prompt PS1 also to one of the standard streams, so that gets included into a file.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 23 at 17:40

























answered Apr 23 at 16:55









Sergiy Kolodyazhnyy

64.6k9129282




64.6k9129282







  • 1




    Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
    – glenn jackman
    Apr 23 at 17:32












  • 1




    Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
    – glenn jackman
    Apr 23 at 17:32







1




1




Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
– glenn jackman
Apr 23 at 17:32




Good answer. As we can see, that exec invocation is much more useful in a non-interactive script.
– glenn jackman
Apr 23 at 17:32

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1027510%2fwhat-does-exec-1-var-opt-log-my-logs-myprog-date-ymd-hms-log-21%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491