/~/.profile and alias-definitions

Message
Author
User avatar
Duliwi
Posts: 1174
Joined: Sun Jul 07, 2019 9:34 am

/~/.profile and alias-definitions

#1 Post by Duliwi »

Hello,

This is a question for interest and learing to understand bash and MX-Linux.

I read a book about bash.
In the chapter "customise the environment" in this book it is said, that during the login one of the following files are loaded:
/~/.bash_profile or /~/.bash_login or /~/.profile .

I have only found /~/.profile .

According to the book, this file is also used for creating alias'es.

To see what alias'es I have in MX-19 Xfce, I have tried this:

Code: Select all

$ alias
alias ag='apt-get update;apt-get dist-upgrade'
alias agc='apt-get clean'
alias agd='apt-get dist-upgrade'
alias agu='apt-get update'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -lh'
alias ls='ls --color=auto'
So my expectation was, to find this alias definitions in the file /~/.profile . But this is not true:

When I open the file /~/.profile, I can not find the definition of this alias'es there:

Code: Select all

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
And the alias'es are also not defined in /etc/profile:

Code: Select all

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
I am confused now. Do I miss anything? Maybe the book is already too old and not up-to-date?
Where can I find the definition of the alias'es above?

Would be interested in an answer. Thank you.

User avatar
dolphin_oracle
Developer
Posts: 22023
Joined: Sun Dec 16, 2007 12:17 pm

Re: /~/.profile and alias-definitions

#2 Post by dolphin_oracle »

our default aliases are in .bashrc
http://www.youtube.com/runwiththedolphin
lenovo ThinkPad X1 Extreme Gen 4 - MX-23
FYI: mx "test" repo is not the same thing as debian testing repo.

User avatar
Duliwi
Posts: 1174
Joined: Sun Jul 07, 2019 9:34 am

Re: /~/.profile and alias-definitions

#3 Post by Duliwi »

Thank you. This gives me new confuses:

I have found the alias

Code: Select all

alias l='ls -CF'
alias la='ls -A'
alias ll='ls -lh'
alias ls='ls --color=auto'
... in .bashrc now.

But according the book alias'es in .bashrc are only loaded, if we start a subshell. (?)

So when I start the command ...

Code: Select all

$ alias
alias ag='apt-get update;apt-get dist-upgrade'
alias agc='apt-get clean'
alias agd='apt-get dist-upgrade'
alias agu='apt-get update'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -lh'
alias ls='ls --color=auto'
... without a subshell my expectation was, that it will not show me this ' l* ' alias'es. But it does.
And I also have not found a "source .bashrc" command in /~/.profile . (?)

User avatar
dolphin_oracle
Developer
Posts: 22023
Joined: Sun Dec 16, 2007 12:17 pm

Re: /~/.profile and alias-definitions

#4 Post by dolphin_oracle »

bash loads .bashrc when you start bash. which is the default shell. and everytime you open a terminal, its processed.
http://www.youtube.com/runwiththedolphin
lenovo ThinkPad X1 Extreme Gen 4 - MX-23
FYI: mx "test" repo is not the same thing as debian testing repo.

User avatar
handy
Posts: 614
Joined: Mon Apr 23, 2018 2:00 pm

Re: /~/.profile and alias-definitions

#5 Post by handy »

~/.bashrc has been where the aliases have been stored in every distro I've looked at & used since 2005.

The user can make a special file that holds their aliases if they want to ~/.bash_aliases is popular on the web these days, but there are other ways too...

Perhaps the book that the OP has been reading is using redundant (old) data in this regard? Linux never stops changing (for better & worse). ;)
MSI: MAG B560 TORP', i5, RAM 16GB, GTX 1070 Ti 12GB, M2 238GB + USB, MX-23 Fb to Openbx
Lenovo: Ideapad 520S, i5, RAM 8GB, GPU i620, HDD 1TB, MX-23 Fb - Openbx
Clevo: P150SM-A, i7, RAM 16GB, nVidia 8600, 2x 1TB HDD & M.2 256 GB, MX-23 Fb - Openbx

User avatar
figueroa
Posts: 1097
Joined: Thu Dec 20, 2018 11:20 pm

Re: /~/.profile and alias-definitions

#6 Post by figueroa »

And, if you'd like to keep your aliases neatly in a file .bash_aliases, add the following to your .bashrc if it doesn't already exist there:

Code: Select all

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
The danger of aliases is that the user learns the aliases and becomes crippled without them. However, I use a fairly extensive number of aliases, cultivated during nearly 40 years of using Unix and Linux.

Strongly recommended aliases:

Code: Select all

alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'
The -i switch will warn/ask you before deleting or overwriting a file. It will save you over and over again. It can, however, be a bit of a nuisance, but the user can override the -i switch from the commandline by using the -f switch. For example,

Code: Select all

rm -f temp1
will not ask you to confirm the decision to remove file temp1.
Andy Figueroa
Using Unix from 1984; GNU/Linux from 1993

Outlander
Posts: 33
Joined: Thu May 12, 2022 9:44 pm

Re: /~/.profile and alias-definitions

#7 Post by Outlander »

I would advise against using .profile. It is only used when you start a login shell (such as with ssh or when logging into a console) and most display managers don't load it. You can source .profile in .xsessionrc and it will load. For aliases, putting them in .bashrc or .bash_aliases should be fine though.

User avatar
Duliwi
Posts: 1174
Joined: Sun Jul 07, 2019 9:34 am

Re: /~/.profile and alias-definitions

#8 Post by Duliwi »

Thank you all.

I guess I have misunderstood the book.
It is said, that when starting a subshell, then the commands from ".bashrc" are loaded.

But I was not aware, that when I type in any command into the terminal, that I then am starting a subshell and so the alias'es from .bashrc are loaded.

Edit:
OK. I have now also found the other alias'es definitions:

Code: Select all

#apt-get
alias agu="apt-get update"
alias agd="apt-get dist-upgrade"
alias agc="apt-get clean"
alias ag="apt-get update;apt-get dist-upgrade"
They are in ...
/etc/bash.bashrc

But the file "/etc/bash.bashrc" is not mentioned in the book. (old Book about bash 3.0; 350 pages.)

Post Reply

Return to “General”