1

A simple example:

~/.bashsrc:

alias test1='ls'

Then we run alias test2='source ~/.bashsrc && test1'.

If we run test2, we will get error: test1: command not found.

How to fix this issue? Or is there any way to realize it without change of bash file.

CC GG
  • 11
  • 1
  • Does this answer your question? [Define alias that references other aliases](https://stackoverflow.com/questions/32500481/define-alias-that-references-other-aliases) – totok Aug 13 '20 at 08:10
  • Use functions instead of aliases. `alias test1=ls` can be replaced by the function `test1() { ls; }`. – Socowi Aug 13 '20 at 08:34
  • @CCGG: When reproducing your example, I found that doing it as `alias test2='source ~/.bashsrc ; test1'` would work. I don't know why `&&` does not work. My guess is that with `&&`, bash tries to resolve all aliases in the compound command, before it executes everything. This would at least explain the behaviour you are observing, but I don't know **why** bash is doing this. After all, `;` is a statement separator, and `&&` is also a statement separator. – user1934428 Aug 13 '20 at 08:42
  • If whatever is being `source`d returns a nonzero exit status (could be something like the last command is a `grep` which finds no matches, or whatever) then of course `&&` will not run the code to the right. But it doesn't really work with `;` either; you probably didn't exit the shell where you were testing, so it had already defined the alias when you tried a second time. – tripleee Aug 13 '20 at 08:55
  • Pretty sure this is a duplicate. The problem is that Bash expands aliases while parsing the line, and so the `source` has not yet happened when it tries to evaluate `test1`. – tripleee Aug 13 '20 at 08:59

0 Answers0