为什么“历史”?当命令来自文件时,tcsh 中不输出任何内容?

发布于 2025-01-03 16:20:46 字数 533 浏览 4 评论 0原文

当我们在 tcsh 中输入“history”时,我们可以看到历史命令列表,如下所示:

ubuntu:~> echo a
a
ubuntu:~> history
     1  9:20    echo a
     2  9:20    history

但是,如果我们将命令存储在文件“commands.txt”中,

echo a
history

通过以下方式将该文件的内容重定向到 tcsh

tcsh < commands.txt

并且我们可以 看到的只是:

a

为什么会发生这种情况?为什么 shell 提示符也不属于输出的一部分?

顺便说一句,它实际上适用于 bash,你只需要像这样打开 history 选项

set -o history
echo a
history

When we type "history" in tcsh, we can see a list of history commands, like this:

ubuntu:~> echo a
a
ubuntu:~> history
     1  9:20    echo a
     2  9:20    history

However, if we store in commands in a file "commands.txt"

echo a
history

And we redirect the content of this file into tcsh by

tcsh < commands.txt

What we can see is only:

a

Why does this happen? And why shell prompt isn't part of the output either?

BTW, it actually works for bash, you just need to turn on history option like this

set -o history
echo a
history

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小兔几 2025-01-10 16:20:46

好吧,这可能不是为什么问题的完美答案。但至少它为您提供了一些信息,这些信息不在脚本中使用历史命令。


http://www.tldp.org/LDP/abs/html/special -chars.html
搜索“历史”你会发现:

请注意,在脚本中,历史记录机制被禁用。

http://tldp.org/LDP/abs/html/histcommands.html 您可以在页面末尾找到:

Unfortunately, the Bash history tools find no use in scripting.

#!/bin/bash
# history.sh
# A (vain) attempt to use the 'history' command in a script.

history                      # No output.

var=$(history); echo "$var"  # $var is empty.

# History commands disabled within a script.

bash$ ./history.sh (no output)

well this maybe not a perfect answer to the question why. but at least it gives you some info, that don't use history command in a script.

in
http://www.tldp.org/LDP/abs/html/special-chars.html
search "history" you find:

Note that within a script, the history mechanism is disabled.

in http://tldp.org/LDP/abs/html/histcommands.html you can find at the end of the page:

Unfortunately, the Bash history tools find no use in scripting.

#!/bin/bash
# history.sh
# A (vain) attempt to use the 'history' command in a script.

history                      # No output.

var=$(history); echo "$var"  # $var is empty.

# History commands disabled within a script.

bash$ ./history.sh (no output)
相思故 2025-01-10 16:20:46

history 命令是 tcsh 内置命令,因此其行为与普通命令不同。根据“Csh 编程被认为有害”的咆哮,第 2a 节说

<块引用>

您无法以多种合理的方式将[内置函数]组合在一起。

我不确定这是否是一种合理的方式。并且脚本不会打印命令提示符。

The history command is a tcsh built-in so doesn't behave like a normal command. Per the "Csh Programming Considered Harmful" rant, section 2a says

You can't put [built-ins] together in many reasonable ways.

And I am not sure its even a reasonable way. And scripts don't print the command prompts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文