Redirect to file, including current prompt line
up vote
4
down vote
favorite
How to include the redirected command itself to the output file redirection? For example
echo "hello!">output.txt
I want the content of output file like this:
echo "hello!">output.txt
hello!
io-redirection
add a comment |
up vote
4
down vote
favorite
How to include the redirected command itself to the output file redirection? For example
echo "hello!">output.txt
I want the content of output file like this:
echo "hello!">output.txt
hello!
io-redirection
Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15
Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
How to include the redirected command itself to the output file redirection? For example
echo "hello!">output.txt
I want the content of output file like this:
echo "hello!">output.txt
hello!
io-redirection
How to include the redirected command itself to the output file redirection? For example
echo "hello!">output.txt
I want the content of output file like this:
echo "hello!">output.txt
hello!
io-redirection
io-redirection
edited Nov 10 at 14:43
ctrl-alt-delor
9,87031954
9,87031954
asked Nov 10 at 14:11
mandrake00
243
243
Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15
Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39
add a comment |
Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15
Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39
Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15
Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15
Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39
Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39
add a comment |
4 Answers
4
active
oldest
votes
up vote
12
down vote
You may want one of two things here.
Using
tee
to get the result of theecho
sent to the terminal as well as to the file:$ echo 'hello!' | tee output
hello!
$ cat output
hello!Using
script
to capture the whole terminal session:$ script
Script started, output file is typescript
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done, output file is typescript$ cat typescript
Script started on Sat Nov 10 15:15:06 2018
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done on Sat Nov 10 15:15:37 2018
If this is not what you are asking for, then please clarify your question.
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
@mandrake00 isn't that exactly what option 2 gives you?script
should do what you need.
– terdon♦
Nov 10 at 14:37
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
1
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
4
You can suppress the lines about when it was started and stopped by usingscript -q
– Panki
Nov 10 at 14:56
|
show 2 more comments
up vote
5
down vote
In the genearality you're asking for, this is not possible.
To achieve this with additional tools (akin to script
(1)), it would be necessary for that program, observing your shell, that in
command >foo
foo
is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print
command >foo
to the terminal?
From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).
If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...
add a comment |
up vote
4
down vote
You can also pipe the whole shell:
$ sh -i |& tee sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
-i
is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |&
pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |
). Of course, you could also use &>
or 2>&1 >
if you just want to redirect to a file and nothing more. Anyways, sh.log
here contains everything.
$ cat sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
add a comment |
up vote
2
down vote
If it's acceptable for you to drop the output filename, I think the following is the easiest way:
command='echo "hello!"'
(echo "$command"; eval "$command") >output.txt
But if you need to print the filename then you could use the following:
command='echo "hello!"'
(echo "$command >output.txt"; eval "$command") >output.txt
You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:
output="output.txt"
command='echo "hello!"'
(echo "$command >$output"; eval "$command") >$output
This way you avoid the risk of having a difference in the printed filename and the actual filename.
1
Generalizing as a function:f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call:f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
You may want one of two things here.
Using
tee
to get the result of theecho
sent to the terminal as well as to the file:$ echo 'hello!' | tee output
hello!
$ cat output
hello!Using
script
to capture the whole terminal session:$ script
Script started, output file is typescript
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done, output file is typescript$ cat typescript
Script started on Sat Nov 10 15:15:06 2018
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done on Sat Nov 10 15:15:37 2018
If this is not what you are asking for, then please clarify your question.
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
@mandrake00 isn't that exactly what option 2 gives you?script
should do what you need.
– terdon♦
Nov 10 at 14:37
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
1
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
4
You can suppress the lines about when it was started and stopped by usingscript -q
– Panki
Nov 10 at 14:56
|
show 2 more comments
up vote
12
down vote
You may want one of two things here.
Using
tee
to get the result of theecho
sent to the terminal as well as to the file:$ echo 'hello!' | tee output
hello!
$ cat output
hello!Using
script
to capture the whole terminal session:$ script
Script started, output file is typescript
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done, output file is typescript$ cat typescript
Script started on Sat Nov 10 15:15:06 2018
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done on Sat Nov 10 15:15:37 2018
If this is not what you are asking for, then please clarify your question.
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
@mandrake00 isn't that exactly what option 2 gives you?script
should do what you need.
– terdon♦
Nov 10 at 14:37
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
1
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
4
You can suppress the lines about when it was started and stopped by usingscript -q
– Panki
Nov 10 at 14:56
|
show 2 more comments
up vote
12
down vote
up vote
12
down vote
You may want one of two things here.
Using
tee
to get the result of theecho
sent to the terminal as well as to the file:$ echo 'hello!' | tee output
hello!
$ cat output
hello!Using
script
to capture the whole terminal session:$ script
Script started, output file is typescript
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done, output file is typescript$ cat typescript
Script started on Sat Nov 10 15:15:06 2018
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done on Sat Nov 10 15:15:37 2018
If this is not what you are asking for, then please clarify your question.
You may want one of two things here.
Using
tee
to get the result of theecho
sent to the terminal as well as to the file:$ echo 'hello!' | tee output
hello!
$ cat output
hello!Using
script
to capture the whole terminal session:$ script
Script started, output file is typescript
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done, output file is typescript$ cat typescript
Script started on Sat Nov 10 15:15:06 2018
$ echo 'hello!' >output
$ cat output
hello!
$ exit
Script done on Sat Nov 10 15:15:37 2018
If this is not what you are asking for, then please clarify your question.
edited Nov 10 at 16:21
Cristian Ciupitu
2,05911621
2,05911621
answered Nov 10 at 14:20
Kusalananda
115k15218351
115k15218351
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
@mandrake00 isn't that exactly what option 2 gives you?script
should do what you need.
– terdon♦
Nov 10 at 14:37
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
1
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
4
You can suppress the lines about when it was started and stopped by usingscript -q
– Panki
Nov 10 at 14:56
|
show 2 more comments
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
@mandrake00 isn't that exactly what option 2 gives you?script
should do what you need.
– terdon♦
Nov 10 at 14:37
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
1
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
4
You can suppress the lines about when it was started and stopped by usingscript -q
– Panki
Nov 10 at 14:56
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
Hi! Nanda, Thanks. I know tee. But I want the output file to include the redirect command that we type in, to be in the output file as well. When I do "cat output", I want to see "echo 'helo'>output" before the line "helo". Hope you understood.
– mandrake00
Nov 10 at 14:26
@mandrake00 isn't that exactly what option 2 gives you?
script
should do what you need.– terdon♦
Nov 10 at 14:37
@mandrake00 isn't that exactly what option 2 gives you?
script
should do what you need.– terdon♦
Nov 10 at 14:37
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
Thank you. The typescript output file has many unwanted lines such as "script started at ..." and "<cr><cr> etc. I was looking for a simple method so that when I do echo 'helo'>out.txt this would give a file with just 2 lines, i.e. echo 'helo'>out.txt n helo
– mandrake00
Nov 10 at 14:45
1
1
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
Do you want this to happen for every single command you ever type in any terminal? Just for specific commands? Please edit your question and give us more specific requirements.
– terdon♦
Nov 10 at 14:46
4
4
You can suppress the lines about when it was started and stopped by using
script -q
– Panki
Nov 10 at 14:56
You can suppress the lines about when it was started and stopped by using
script -q
– Panki
Nov 10 at 14:56
|
show 2 more comments
up vote
5
down vote
In the genearality you're asking for, this is not possible.
To achieve this with additional tools (akin to script
(1)), it would be necessary for that program, observing your shell, that in
command >foo
foo
is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print
command >foo
to the terminal?
From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).
If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...
add a comment |
up vote
5
down vote
In the genearality you're asking for, this is not possible.
To achieve this with additional tools (akin to script
(1)), it would be necessary for that program, observing your shell, that in
command >foo
foo
is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print
command >foo
to the terminal?
From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).
If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...
add a comment |
up vote
5
down vote
up vote
5
down vote
In the genearality you're asking for, this is not possible.
To achieve this with additional tools (akin to script
(1)), it would be necessary for that program, observing your shell, that in
command >foo
foo
is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print
command >foo
to the terminal?
From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).
If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...
In the genearality you're asking for, this is not possible.
To achieve this with additional tools (akin to script
(1)), it would be necessary for that program, observing your shell, that in
command >foo
foo
is the name of a file, and create it. Also, the shell would try to create it, so there's conflict already. Also, what if the program would print
command >foo
to the terminal?
From inside the shell, well, the shell's redirection process is extremely primitive from a programming point of view (try to find a C lecture where they build a primitive shell themselves, it is really surprising).
If you really need this, you would have to hack your own shell to intercept redirections and pass the command into the target file. But there are probably a lot of nasty issues in the details...
answered Nov 10 at 16:16
stefan
475312
475312
add a comment |
add a comment |
up vote
4
down vote
You can also pipe the whole shell:
$ sh -i |& tee sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
-i
is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |&
pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |
). Of course, you could also use &>
or 2>&1 >
if you just want to redirect to a file and nothing more. Anyways, sh.log
here contains everything.
$ cat sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
add a comment |
up vote
4
down vote
You can also pipe the whole shell:
$ sh -i |& tee sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
-i
is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |&
pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |
). Of course, you could also use &>
or 2>&1 >
if you just want to redirect to a file and nothing more. Anyways, sh.log
here contains everything.
$ cat sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
add a comment |
up vote
4
down vote
up vote
4
down vote
You can also pipe the whole shell:
$ sh -i |& tee sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
-i
is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |&
pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |
). Of course, you could also use &>
or 2>&1 >
if you just want to redirect to a file and nothing more. Anyways, sh.log
here contains everything.
$ cat sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
You can also pipe the whole shell:
$ sh -i |& tee sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
-i
is needed to keep the shell interactive despite stdout not being a terminal. bash and zsh also support that option. |&
pipes stdout and stderr; it works with zsh and bash, but not sh (there, you'd need 2>&1 |
). Of course, you could also use &>
or 2>&1 >
if you just want to redirect to a file and nothing more. Anyways, sh.log
here contains everything.
$ cat sh.log
sh-4.4$ hello
sh: hello: command not found
sh-4.4$ echo hi
hi
sh-4.4$ exit
edited Nov 10 at 21:45
answered Nov 10 at 16:20
JoL
80839
80839
add a comment |
add a comment |
up vote
2
down vote
If it's acceptable for you to drop the output filename, I think the following is the easiest way:
command='echo "hello!"'
(echo "$command"; eval "$command") >output.txt
But if you need to print the filename then you could use the following:
command='echo "hello!"'
(echo "$command >output.txt"; eval "$command") >output.txt
You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:
output="output.txt"
command='echo "hello!"'
(echo "$command >$output"; eval "$command") >$output
This way you avoid the risk of having a difference in the printed filename and the actual filename.
1
Generalizing as a function:f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call:f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
add a comment |
up vote
2
down vote
If it's acceptable for you to drop the output filename, I think the following is the easiest way:
command='echo "hello!"'
(echo "$command"; eval "$command") >output.txt
But if you need to print the filename then you could use the following:
command='echo "hello!"'
(echo "$command >output.txt"; eval "$command") >output.txt
You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:
output="output.txt"
command='echo "hello!"'
(echo "$command >$output"; eval "$command") >$output
This way you avoid the risk of having a difference in the printed filename and the actual filename.
1
Generalizing as a function:f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call:f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
add a comment |
up vote
2
down vote
up vote
2
down vote
If it's acceptable for you to drop the output filename, I think the following is the easiest way:
command='echo "hello!"'
(echo "$command"; eval "$command") >output.txt
But if you need to print the filename then you could use the following:
command='echo "hello!"'
(echo "$command >output.txt"; eval "$command") >output.txt
You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:
output="output.txt"
command='echo "hello!"'
(echo "$command >$output"; eval "$command") >$output
This way you avoid the risk of having a difference in the printed filename and the actual filename.
If it's acceptable for you to drop the output filename, I think the following is the easiest way:
command='echo "hello!"'
(echo "$command"; eval "$command") >output.txt
But if you need to print the filename then you could use the following:
command='echo "hello!"'
(echo "$command >output.txt"; eval "$command") >output.txt
You can introduce a variable for the filename in order to make sure that it is the same in the two occurrences:
output="output.txt"
command='echo "hello!"'
(echo "$command >$output"; eval "$command") >$output
This way you avoid the risk of having a difference in the printed filename and the actual filename.
edited Nov 10 at 20:04
wjandrea
462413
462413
answered Nov 10 at 15:25
Erwan
1664
1664
1
Generalizing as a function:f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call:f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
add a comment |
1
Generalizing as a function:f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call:f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
1
1
Generalizing as a function:
f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call: f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
Generalizing as a function:
f() command="$1"; file="$2"; (printf '%s >%s' "$command" "$file"; eval "$command") >"$file";
Then call: f 'echo "Hello!"' output.txt
– wjandrea
Nov 10 at 19:20
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480956%2fredirect-to-file-including-current-prompt-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have a specific need for this?
– wjandrea
Nov 10 at 19:15
Yes. I have a specific need. I am creating a log file.
– mandrake00
Nov 13 at 11:39