Wednesday, May 15, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1698  / 1 Year ago, fri, april 21, 2023, 8:17:41
root@puppetclient-ubuntu:/home/azureuser#cat /etc/.bash_aliases
alias extend_shutdown_15='bash extend_shutdown.sh 15 ; bash /bin/max_timetrack.sh'
alias extend_shutdown_30='bash extend_shutdown.sh 30 ; bash /bin/max_timetrack.sh'
alias extend_shutdown_60='bash extend_shutdown.sh 60 ; bash /bin/max_timetrack.sh'

#try to automate and execute the "source /etc/.bash_aliases" through shell script but it is not working means changes are not effecting.


root@puppetclient-ubuntu:/home/azureuser# cat alias.sh
#!/bin/bash
source ~/.bash_aliases

when I do source /etc/.bash_aliases the alias custom commands are available only for root user and it is not available for all others expect "root" if I do execute one time it should always available for the all users. Please help me to fix.Thanks.


More From » source

 Answers
0

First, aliases are temporary and valid only for the currently running shell. When you run the alias.sh file, it executes in its own shell, not in the shell it was run from. So the script sources ~/.bash_aliases (why this file and not /etc/.bash_aliases? I thought you want the latter?), but once the script ends, the shell also exits and whatever aliases could have been defined in the ~/.bash_aliases file, they are gone.


That's the difference between running a shell script and sourcing it. If you run a shell script, it runs in it's own shell, so any changes to environment, aliases and the similar done in the script do not affect the parent shell (the shell the script was started from). But when you source the same script, it executes in the current shell, so all changes it makes stay after the script finishes (of course they stay as long as the shell lives - if you exit the shell, all changes are gone again).


Taking this into account, if you want aliases from the file /etc/.bash_aliases to be available to any user in any shell they run, the file must be sourced from /etc/bash.bashrc file. This file is executed whenever any shell (from any user) starts and is commonly used to set the initial environment.


So if you add the following line at the end of /etc/bash.bashrc (of course you must do it as root, to be able to edit the file):


source /etc/.bash_aliases

you should get what you want.


The alias.sh file isn't needed at all.


[#1179] Saturday, April 22, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cisccla

Total Points: 66
Total Questions: 134
Total Answers: 115

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
cisccla questions
Tue, Nov 16, 21, 08:40, 3 Years ago
Thu, Sep 1, 22, 19:53, 2 Years ago
Thu, Dec 2, 21, 15:29, 3 Years ago
;