yoon
yoon

Reputation: 1405

How can I use aliases in bash scripts without changing the scripts?

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?

Upvotes: 4

Views: 3024

Answers (1)

oguz ismail
oguz ismail

Reputation: 50805

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

Upvotes: 4

Related Questions