[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Some Bash Alias Statements Work, Others Don't.



On Tue, Oct 29, 2019 at 08:50:05AM -0400, The Wanderer wrote:
> Can you give some examples of aliases which work and which don't? By
> which I mean, paste in the alias-definition line for each one.

Even better, paste an actual terminal session showing your prompts,
the commands you are typing, and their output.

For example,

wooledg:~$ type cls
cls is aliased to `tput clear; tput cup 99 0'
wooledg:~$ type clearscreen
bash: type: clearscreen: not found
wooledg:~$ clearscreen
bash: clearscreen: command not found

That level of detail is the bare minimum starting point.

> At first blush from the given description, my guess would be that bash
> is now for some reason not actually reading your .bashrc, but is instead
> getting its alias definitions from somewhere else, which happens to
> include some but not all of the definitions your own file specifies.

Yup.  In addition to seeing the commands and their output, we need
to know some background information:

1) How did you log in?
2) How did you open this terminal?
3) Show your basic dot files.

For example, when answering #1, you might say "this is a virtual machine
running Debian under a Windows 10 host, and I access it by ssh to
localhost".  Or, "I'm running GNOME/Wayland, and I login via gdm3".
Or, "I login on a text console and run startx".

When answering #2, it might be helpful to show the actual command line
of your terminal.  For example,

wooledg:~$ ps -fp $PPID
UID        PID  PPID  C STIME TTY          TIME CMD
wooledg   1502  1280  1 09:03 tty1     00:00:00 rxvt -font 7x13

What we *really* want to know is whether your shell is a login shell,
or a nonlogin shell, because that determines which dot files it reads.
So, showing the shell's command line is also helpful.

wooledg:~$ ps -fp $$
UID        PID  PPID  C STIME TTY          TIME CMD
wooledg   1511  1502  0 09:03 pts/26   00:00:00 bash

For #3, you kinda need to know the names of the basic dot files of
your shell.  These are in the documentation, but most people never
read the documentation.

For bash, what we want is:

ls -ld ~/.bash* ~/.profile /etc/profile /etc/bash.bashrc

wooledg:~$ ls -ld ~/.bash* ~/.profile /etc/profile /etc/bash.bashrc
-rw-r--r-- 1 root    root     1994 Jan 24  2019 /etc/bash.bashrc
-rw-r--r-- 1 root    root      805 May 23 11:55 /etc/profile
-rw------- 1 wooledg voice   38416 Oct 24 15:59 /home/wooledg/.bash_history
-rw-r--r-- 1 wooledg wooledg    62 Apr 28  2014 /home/wooledg/.bash_logout
-rw-r--r-- 1 wooledg wooledg  3760 Sep 13 14:17 /home/wooledg/.bashrc
-rw-r--r-- 1 wooledg wooledg  1090 Sep 16 11:14 /home/wooledg/.profile

Again, this is the *minimum* acceptable level of detail.  Once we
know which dot files are going to be used, then we might ask to see
one of them.

In my example, I showed a nonlogin bash shell running in a traditional
X terminal emulator.  So, in my case, the shell in that terminal will
have read /etc/bash.bashrc and ~/.bashrc *only*.  It will not have read
/etc/profile or ~/.profile because nonlogin shells don't read those.

In the case of someone running a login shell (either because they're
ssh-ing in, or because their window manager is configured to run a
terminal that asks for a login shell, e.g. "xterm -ls"), the shell
would read /etc/profile and one of (~/.bash_profile or ~/.bash_login
or ~/.profile).  The end user is expected to ensure that their ~/.bashrc
file is sourced by their profile.

It may also be helpful to approach the problem from the other direction.
For example,

"I placed two aliases for `foo' and `bar' in my ~/.bash_aliases file.
Why aren't they being used?"

Then we can answer that ("Bash does not read that file.  If you would
like it to do so, you need to source it from ~/.bashrc which is what
it actually reads.")

In any case, some basic competence in the field of revealing the
relevant details of one's problem is required.


Reply to: