On Thu, Jan 12, 2023 at 03:43:15AM +0000, anonymous4feedb...@outlook.com wrote: > Dear dir or madam, > > For the following script: > alias a1='echo ' > alias a2=a1 > alias foo=bar > a2 foo > bash outputs ‘foo’, instead of ‘bar’. > > GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) on archlinux as > windows subsystem for linux
First thing first: aliases are not used in scripts, unless bash is invoked in POSIX mode, or the "expand_aliases" shopt is enabled. So either this wasn't really a script (i.e. you ran these commands at an interactive shell), or else this wasn't the *entire* script. Second thing: the command in question here is "a2 foo", where a2 is an alias. But the a2 alias doesn't end with a space. So there's no reason to expect that the special "alias ends with a space" processing would occur here. If you used "a1 foo" then it *would* occur. You're expecting an alias that aliases an alias to inherit the special hacks of the alias that it's aliasing. It doesn't. If you want a2 to have the space-hack behavior, put a space after it. Here's a working version: unicorn:~$ cat foo #!/bin/bash shopt -s expand_aliases alias a1='echo ' alias a2='a1 ' alias foo=bar a1 foo one a2 foo two unicorn:~$ ./foo bar one bar two In my opinion, the only bug here is in your script, not in bash. Also my opinion: aliases are disabled in bash scripts for a reason. They're ugly and horrible, and they don't promote good programming practices. Whatever you're trying to do here, I doubt aliases are the best way to do it. Consider using functions instead.