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

0

1 Answer 1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.