[Solved] File "aliases.sh" in /etc/profile.d doesn't work

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
hshl
Level 1
Level 1
Posts: 37
Joined: Mon Aug 14, 2023 4:24 pm
Location: Berlin

[Solved] File "aliases.sh" in /etc/profile.d doesn't work

Post by hshl »

Hi all,

On my PC Linux Mint "Victoria" is running and I would like to set aliases that apply equally to all users of my PC. This should be very easily done by creating an *.sh file in the directory /etc/profile.d. According to the file /etc/profile it should work since it refers to the /etc/profile.d directory:

Code: Select all

$ cat /etc/profile
[...]
if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
So I created an "aliases.sh" file with the following content in the /etc/profile.d directory:

Code: Select all

$ cat /etc/profile.d/aliases.sh
alias ll='ls -la'
alias g='grep'
alias ..='cd ..'
alias s='sudo'
alias update='sudo apt update'
The alias command brings only the aliases defined by Linux Mint:

Code: Select all

$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
Why are my defined aliases in /etc/profile.d/aliases.sh ignored?

Thanks a lot
Last edited by hshl on Sun Dec 31, 2023 3:44 pm, edited 2 times in total.
Hawaiihemd
Level 4
Level 4
Posts: 409
Joined: Fri Sep 25, 2020 12:42 pm

Re: File "aliases.sh" in /etc/profile.d doesn't work

Post by Hawaiihemd »

I am a total noob when it comes to bash and I could be way off here.
But I believe the file names in these .d directories need to start with numbers.
So, try renaming your file 99-aliases.sh for example.
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: File "aliases.sh" in /etc/profile.d doesn't work

Post by Shiva »

hshl wrote: Sun Dec 31, 2023 4:40 am Why are my defined aliases in /etc/profile.d/aliases.sh ignored?
They are not ignored. They probably run fine ... in their own subshell (= the subshell of the aliases.sh script)

If you want them to be active in other shells, you have to source those shells.

For bash, add :

Code: Select all

source /etc/profile.d/aliases.sh
at the end of your .bashrc file (same for other users) and they'll work.
hshl
Level 1
Level 1
Posts: 37
Joined: Mon Aug 14, 2023 4:24 pm
Location: Berlin

Re: File "aliases.sh" in /etc/profile.d doesn't work

Post by hshl »

Thanks so far,

@Hawaiihemd: the number in front of the file is just to determine the order the files are used when Linux reads the files in the directory. Otherwise the script in /etc/profile doesn't make sense?!
@ Shiva: That's a solution, thanks. But it's quite similar as if the alias is directly defined in the ./bashrc of each user (and that's not what I want). So I wonder why the other files in /etc/profile.d seems to work for the whole system while the aliases.sh doesn't. Can I avoid that the aliases of the aliases.sh are running in a subshell?

Thanks!
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: File "aliases.sh" in /etc/profile.d doesn't work

Post by Shiva »

hshl wrote: Sun Dec 31, 2023 11:40 am Can I avoid that the aliases of the aliases.sh are running in a subshell?
No, because you have made a script out of them : unless an alias is created locally, in order to simplify syntax or to override an existing alias during a script for example, it's not a good idea to use that method because scripts run in subshells.

The others scripts in /etc/profile.d/ run at startup, at system level (root) and mostly define environment variables.

Basically your aliases are only usable by root during start-up.

However, if I correctly understand what you want, which is creating kinda "public" aliases available for everyone, just add them at the end of the /etc/bash.bashrc file. Of course, you need elevated privileges to modify that file.

More here if you want to understand about
The Bash Shell Startup Files :
https://www.linuxfromscratch.org/blfs/v ... ofile.html

Subshells :
https://tldp.org/LDP/abs/html/subshells.html
hshl
Level 1
Level 1
Posts: 37
Joined: Mon Aug 14, 2023 4:24 pm
Location: Berlin

Re: File "aliases.sh" in /etc/profile.d doesn't work

Post by hshl »

Good explanations Shiva, thanks a lot!
Post Reply

Return to “Scripts & Bash”