~$ bash --version
GNU bash, version 5.1.12(1)-release (x86_64-pc-linux-gnu)
~$ alias bab=python
~$ $(echo bab)
bash: bab: command not found
I'd expect bab to be turned to "python", but it seems it's not.
~$ $(echo alias)
alias bab='python'
alias ls='ls --color=auto'
~$ bab
Python 3.10.1 (main, Dec 11 2021, 17:22:55) [GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The alias is recognized though, and works outside the interpolation. Why won't it work inside the interpolation?
eval
works:
~$ eval $(bab)
Python 3.10.1 (main, Dec 11 2021, 17:22:55) [GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
I still wonder why
eval $(bab)
you would evaluate the output ofbab
. Yes, it runspython
, but that's before it gets toeval
. What is it that you are trying to achieve? – Kusalananda Jan 20 '22 at 16:32eval $(bab)
isn't doing what you think it's doing - you're seeing thebab
alias running python. When that's completed the results will get passed toeval
. You can see this by running a command in thepython
session such asprint("date\n")
– Chris Davies Jan 20 '22 at 16:33$(echo xyz)
in particular is pretty much useless, same asecho $(zyx)
. But I assume you actually have some other expansion there. – ilkkachu Jan 20 '22 at 17:04eval $(bab)
, or equally,eval $(python)
, and try e.g.print("hello")
in the interpreter, you'll see the output doesn't appear on the screen. Instead, when you exit the interpreter, Bash will complain about not finding the commandhello
... When starting the python interpreter in a command substitution, it's output is captured by the shell. It just prints the command line to the terminal, bypass the command substitution. You probably don't want that. Something likevar=$(python script.py)
would make more sense, of course. – ilkkachu Jan 20 '22 at 17:08$(anything)
would not just runanything
, it would expand the output of that command, split the output on the characters in$IFS
, and apply filename globbing on the resulting strings. Then it would treat the first word as a command. It's unclear why you are attempting to use this syntax. – Kusalananda Jan 20 '22 at 17:35