Score:2

how to add alias to bashrc and load content with sudo?

tr flag

I need to run my script with sudo, because it includes commands for installing programs with apt. So I do this:

sudo ./myscript.sh

With content:

#!/bin/bash

# add alias .bashrc (work)
echo "alias myupgrade='sudo aptitude -y safe-upgrade && sudo updatedb'" | sudo tee -a /home/my_user/.bashrc > /dev/null
# or
# sudo -u my_user bash -c "echo alias myupgrade=\"'sudo aptitude -y safe-upgrade && sudo updatedb'\"" >> ~/.bashrc

# load changes (doesn't work)
sudo -u my_user bash -c "source /home/my_user/.bashrc"

# rest of script (not relevant for the question). e.g:
apt -y install foo bar etc

My script adds the alias to bashrc fine, but the rest doesn't work. I have tried these commands inside my script, without success:

# doesn't work
sudo su - my_user -c "source /home/my_user/.bashrc"
source /home/my_user/.bashrc
source ~/.bashrc
sudo bash --rcfile /home/my_user/.bashrc

Any help (ubuntu 22.04)? Thanks in advance

Update (Workaround):

There is a workaround posted HERE, opening the terminal and running the temporary alias:

# create temporary aliases
alias myupgrade='apt-get update && apt-get dist-upgrade && aptitude -y safe-upgrade && updatedb'
# creating temporary sudo aliases
alias sudo='sudo '
# run temporary aliases (doesn't work inside bash script)
sudo myupgrade

but it doesn't work inside a bash script

acgbox avatar
tr flag
@PabloBianchi I need to run my script with sudo because my script contains installation of programs, not related to the question. It is already explained in the question with details. I invite you to read it again
hr flag
By default, aliases aren't expanded a non-interactive context (i.e. script) anyhow
vanadium avatar
cn flag
How did you check your approach did not work? It will work if the alias is defined in the environment of the target user. If, however, you expect the alias to work from within a script, then your expectation, as steeldriver indicated, is wrong. That does not mean the alias is not defined.
acgbox avatar
tr flag
@vanadium Please read the question. The alias is in bashrc and is added to this file fine with the described command. The only thing that doesn't work is the command to load the alias. If you have doubts about the question, please describe in which part you have doubts, and I will gladly explain it to you.
acgbox avatar
tr flag
@steeldriver any workaround?
hr flag
**if** your first script works (which I don't believe it should, unless you set the `expand_aliases` shell option) then running it with `sudo` should also work **as is**. It can't possibly work with `sudo -u my_user bash -c "source /home/my_user/.bashrc"` because that sources your user's .bashrc file into the `bash -c` child shell, then immediately returns to the parent shell.
Score:4
jp flag

The shell option expand_aliases is needed enabled for aliases to be expanded ... It's enabled by default for interactive shells, but not enabled by default for non-interactive shells(like the one your script is run in by default) ... So,

Either

Change your shebang to:

#!/bin/bash -i

Or, alternatively, run your script file like so:

sudo /bin/bash -i myscript.sh

To run your script in an interactive shell.

Or

Add:

shopt -s expand_aliases

at the top of your script file after the shebang to enable aliases expansion in none-interactive shells.

Then

Source the file containing your alias with full path early in your script before you call the alias like so:

source /home/my_user/.bashrc

Or define your alias early in the same script before you call the alias like so:

alias myupgrade='aptitude -y safe-upgrade && updatedb'

Notice

If you run your script file with sudo like e.g.:

sudo ./myscript.sh

Or:

sudo /bin/bash myscript.sh

Then you don't need to use sudo in any of its included or sourced commands/aliases as they will all run under the user root.

acgbox avatar
tr flag
none of your suggestions work. but thanks for trying
Artur Meinild avatar
vn flag
@acgbox I think it's something with your methodology that isn't working. Everything that Raffa is writing is working for me.
acgbox avatar
tr flag
@ArturMeinild It does not work... But there is good news. It works if the alias is temporary (as I explain in the update workaround), adding `shopt -s expand_aliases` to the script header under the shebang. And that's the only reason why I'll mark the answer as correct.
acgbox avatar
tr flag
And I suppose that when the script finish the part of adds the aliases i have to put `shopt -u expand_aliases` to disable what was enabled in the header. It's right?
Raffa avatar
jp flag
@acgbox That option is not about adding aliases but rather about expanding/using them ... aliases will be added/set fine without it, but you need it to use/execute/interpret them in your script/shell ... That option will only affect the shell you set it in i.e. the one that is launched to interpret the commands in your script and when your script ends then that shell is terminated ,,, So, you don't need to unset it unless you want to disable expanding/interpreting aliases in certain parts of your script which is rarely required ... Therefore, unsettling is not needed in your case.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.