1

This question already has an answer here:

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)?

marked as duplicate by cas, Kusalananda bash Jan 25 '18 at 6:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

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
...
  • Wouldn't it be best to put this in the head of the script, one line after #!/bin/bash, from aesthetic reasons? – pntshere Jan 25 '18 at 7:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.