如何调试 bash 提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8261602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:06:24  来源:igfitidea点击:

How can I debug the bash prompt?

linuxdebuggingbash

提问by fny

I've been editing .bashrcfiles and other init files, and it seems that I've left behind a few code snippets or two that are causing a few errors at the prompt (e.g. file missing), but I can't find them.

我一直在编辑.bashrc文件和其他 init 文件,似乎我留下了一些或两个代码片段,这些代码片段会导致出现一些提示错误(例如文件丢失),但我找不到它们。

How do I debug the prompt to find out what init scripts I've carelessly hacked?

如何调试提示以找出我不小心入侵的初始化脚本?

回答by Alexandre Rafalovitch

Most of the shells have debug flags that show the commands being executed. Bash may even have one that shows a command before expansion of variables and after. Have you tried checking (I believe) -c -x or -X flags and see if they show the information you are looking for.

大多数 shell 都有显示正在执行的命令的调试标志。Bash 甚至可能有一个在变量扩展之前和之后显示命令的命令。您是否尝试过检查(我相信)-c -x 或 -X 标志并查看它们是否显示您正在寻找的信息。

You can set them as first thing in the rc files (most global one) or just pass it down into bash command by invoking it from another shell.

您可以将它们设置为 rc 文件(大多数全局文件)中的第一件事,或者通过从另一个 shell 调用将其传递给 bash 命令。

In fact, if you invoke bash from another shell, you can also use scriptcommand to record everything you see and do into the file, which makes postmortem analysis so much easier.

事实上,如果你从另一个 shell 调用 bash,你也可以使用脚本命令将你看到的和所做的一切记录到文件中,这使得事后分析变得更加容易。

回答by choad rocker

Try invoking bash with the -x flag, then sourcing your .bashrc or .bash_profile or whatever you're using. That ought to be prolix enough to find your problem

尝试使用 -x 标志调用 bash,然后获取 .bashrc 或 .bash_profile 或您正在使用的任何文件。那应该足够长以找到您的问题

ie:

IE:

bash -x
source .bashrc

回答by RobM

The easiest way to get a clean initial state is to SSH into your current host, but instead of letting SSH launch your shell with default settings, you provide an explicit command which prevents .bashrcfrom being read.

获得干净初始状态的最简单方法是通过 SSH 连接到您当前的主机,但不是让 SSH 使用默认设置启动您的 shell,而是提供一个显式命令来防止.bashrc被读取。

ssh -tt localhost /bin/bash --norc

The -ttforces SSH to allocate a TTY, which is what would normally happen when you open a shell connection, but is notdefault when running an explicit command.

-tt强制ssh分配一个TTY,它是当你打开外壳连接东西通常会发生,但运行的明确命令时的默认。

The --norcprevents bash from reading your settings file (since we want to do that ourselves).

--norc防止读你的设置文件(因为我们想要做的是我们自己)抨击。

You should now be at a bash prompt, in a cleanenvironment. This is useful for examining what variable are set to before your .bashrcruns etc. Enable tracing and source your .bashrc:

您现在应该处于 bash 提示符下,处于干净的环境中。这对于检查在.bashrc运行之前设置的变量很有用等。启用跟踪并获取您的.bashrc

set -x   #?Enable tracing
source .bashrc

回答by dimba

Try to see where you've defined prompt - probably it in some dot file in your home directory:

尝试查看您定义提示的位置 - 可能在您的主目录中的某个点文件中:

grep PS1 ~/.*

You can see current value of prompt by just printing it:

您可以通过打印来查看提示的当前值:

echo $PS1

HTH

HTH

回答by Raihan

Check the .bash_historyfile in your home directory to find out what commands you have been running. If you used commands like vi filenameto open the init scripts, it will find them in the command history.

检查.bash_history您的主目录中的 文件以找出您正在运行的命令。如果您使用诸如vi filename打开 init 脚本之类的命令,它将在命令历史记录中找到它们。