2

I am a beginner and have not used Linux for a long time. Now I no longer know what I did then that every time I open the terminal 3 commands are executed, which I no longer need and which no longer work as before. Now my question is: How can I switch off that these commands are executed every time I open the terminal.

2 Answers 2

5

Check the .bashrc file in your home directory for a source command.

This file is read every time your terminal emulator starts and if it contains a source it runs those commands.

You can check this by running:

cat .bashrc | grep source
0
3

TL;DR: diff /etc/skel/.bashrc ~/.bashrc shows changes to ~/.bashrc, and there are other files beisdes ~/.bashrc that can produce the problem you've described.

When unexpected commands run when you start a Bash shell, checking .bashrc as Christos Takaridis says will often reveal the problem. But sometimes that's not enough. There's more to check for than just source commands, and sometimes you'll want to check more files than just .bashrc.

The Bash shell builtin source can also be spelled . (which is actually its more common spelling), so in addition to:

grep source ~/.bashrc

It may also be helpful to run:

grep -F '. ' ~/.bashrc

(Without -F, the grep command treats its pattern as a regular expression, and an unescaped . in a regular expression matches any character, not just a literal ..)

Though, even with the literal space after the ., as shown, that may have some false positives.

If you do find files that are sourced, either with the source command or the . command (in Bash they are synonyms), then I recommend examining their contents before commenting out (with a leading #) or deleting the source or . lines. In them, you may find the command you're looking for, or you may find other . or source commands.

In particular, the default ~/.bashrc file in Ubuntu sources (with .) the file ~/.bash_aliases if it exists. Usually this is used as a convenient place to define aliases, but any Bash commands could go in there and would be run. Sometimes unexpected output turns out to be from commands that aren't alias definitions that have accidentally been placed in ~/.bash_aliases. You will also get output in the form of error messages if an alias definition is accidentally malformed (though if it is well-formed but the text it is defined to expand to is wrong, you won't know that until you actually use the alias).

Another way to check what has been added to or removed from .bashrc is to compare it to the version the system gives newly created user accounts. Usually this is the same as the file you started with before making modifications, though sometimes when Ubuntu is upgraded from one release to another, this file changes (without causing the .bashrc files in existing users' home directories to be changed). This file is /etc/skel/.bashrc (because the contents of the /etc/skel directory are copied into the home directories of newly created user accounts, when the accounts are created in the usual way) and you can see the differences between it and your .bashrc file by running:

diff /etc/skel/.bashrc ~/.bashrc

In one variation on this problem, your ~/.bashrc may contain output-producing commands placed very high up in it, so that they run before the check for whether the current shell is interactive or not. Many non-interactive shells don't run commands from ~/.bashrc at all, but some remote shells--which in Ubuntu includes the shell that runs a single command noninteractively via ssh and that manages an sftp or scp connection--will do so. Since most of what .bashrc is actually used for is inappropriate for such shell, the default .bashrc checks if the currently running shell is interactive and stops if it isn't. If you want something to run in those shells, it must be placed before the interactivity check. Far more often, you don't, but might've put them there anyway. Commands before the interactivity check that produce any output at all will usually prevent sftp and scp from working for that user account until they are removed.

(Or, more rarely, this check might be removed or modified. Outside of Debian and Ubuntu, the default .bashrc may or may not contain such a check. This answer is intended primarily for Ubuntu users.)

In another variation of this problem, the commands you're looking for can't be found in ~/.bashrc and only run in login shells, which includes the shell you get when you log in on a non-graphical virtual console as well as when you ssh into the machine, but does not include the shell you get when you open a terminal window (unless you're configured your terminal application to start a login shell, or unless this is a WSL system in Windows 10). shopt login_shell tells you if the interactive Bash shell you're currently running is a login shell.

In this situation, you should check your ~/.profile, which login shells use:

diff /etc/skel/.profile ~/.profile

In Ubuntu you will not usually have a ~/.bash_profile or ~/.bash_login file. If one of those does exist, it gets used instead of ~/.profile, though it may cause ~/.profile to be used as well by sourcing ~/.profile.

By default, in Ubuntu, ~/.profile sources ~/.bashrc, so even interactive login shells run the commands in ~/.bashrc and the mere presence of the problem in a login shell as well as other shells doesn't mean it necessarily resides elsewhere than ~/.bashrc.

Finally, note that the systemwide counterparts of ~/.bashrc and ~/.profile are /etc/bash.bashrc and /etc/profile, respectively. Those files are rarely modified, and rarely should be modified, but if you do modify them, they will also affect shell startup behavior, in an analogous way.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .