Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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?

like image 682
yoon Avatar asked Sep 05 '25 01:09

yoon


1 Answers

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
like image 114
oguz ismail Avatar answered Sep 07 '25 17:09

oguz ismail