1

I need a A substitue for Bash aliases (something that behaves basically like an alias but isn't an alias).

The reason I need such a substitute is because scripts cannot utilize aliases. That is to say --- an alias works fine when I execute it manually in Bash, but it fails to work when runned as part of a script. The solution I know of is to put the aliases in a temporary file but I don't want that approach.

Is there any substitute / similar command-shortcut I could utilize, some "next generation alias" (to give a metaphor) that will behave just like an alias but will also be naturally accessible for scripts (after its file was sourced)?

1 Answers1

3

In man bash:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt.

So, I think you need to add shopt expand_aliases in your bash script.

Test:

$ cat 1.sh 
#!/bin/bash
alias ll='ls -l'
ll $HOME

$ ./1.sh 
./1.sh: line 3: ll: command not found

$ cat 2.sh 
#!/bin/bash
shopt -s expand_aliases
alias ll='ls -l'
ll $HOME

$ ./2.sh 
total 12
...
Bach Lien
  • 229