4

I know writing

source ~/.bashrc
shopt -s expand_aliases

in a bash script allows to use aliases defined in .bashrc file.

However, I have so many bash scripts, and I cannot change all those scripts.

Is there a way to let my aliases used in all my scripts, with setting env or something?

3
  • 1
    The very simple and straightforward recommended solution is to not use aliases in scripts. Rewrite them as functions or standalone scripts. You are creating more spaghetti by making every script depend on your personal settings, and creating hard-to-debug problems if you ever change your .bashrc to add something which should only actually be done when a new interactive shell is started. Commented Mar 30, 2021 at 5:26
  • @tripleee Thanks for your comment. But in my case, sudo -i should always be used instead of sudo. Some sudo commands might kill the node. So I had to force it to prevent mistakes because not only I am using the servers. Commented Mar 30, 2021 at 5:31
  • 1
    So create /usr/local/bin/sudo with a wrapper to override and dispatch /usr/bin/sudo with this option. For example. Commented Mar 30, 2021 at 5:32

1 Answer 1

4

Put the code that enables aliases and sources .bashrc to another file, assign its path to BASH_ENV, and export BASH_ENV.

$ cat .bashrc
alias dt=date
$ cat my_env.sh
shopt -s expand_aliases
source ~/.bashrc
$ cat my_script
#!/bin/bash
dt
$ export BASH_ENV=my_env.sh
$ ./my_script
Tue Mar 30 07:57:50 +03 2021
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.