'$BASH_VERSION vs /usr/bin/env

I run this to check env:

env | grep bash  

and then got nothing.

So, I think the variable bash is not set in env. However, in a shell script file:

#!/usr/bin/env bash

echo $BASH_VERSION

I got 5.1.16(1)-release.

It seems contradictory because, on the one hand, I think the variable bash must have been set as the shell script file can be executed in a manner as expected; on the other hand, running env | grep bash, I got nothing.

How could the OS locate the app bash-5.1.16 without an env variable being set?



Solution 1:[1]

The $BASH_VERSION is not an environment variable but a shell variable.
You can see defined shell variable with the declare builtin.

declare | grep BASH_VERSION

BASH_VERSION='5.1.4(1)-release'

The env program is used to run a program with modified environment.
Running /usr/bin/env bash without arguments is basically the same as running bash, it's ensure that the program is on the file system and not a shell builtin.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iElden