0

I found the list of many BASH variables, https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html

The description says,

These variables are set or used by Bash, but other shells do not normally treat them specially.

And the first variable is,

BASH: The full pathname used to execute the current instance of Bash.

but when I type BASH in my terminal I get bash: BASH: command not found

I can confirm from echo $0 that I am using bash.

Similarly, when I run BASH_VERSINFO I get the same output, even though the description of the command says,

A readonly array variable (see Arrays) whose members hold version information for this instance of Bash. The values assigned to the array members are as follows:

BASH_VERSINFO[0] The major version number (the release).

BASH_VERSINFO[1] The minor version number (the version).

BASH_VERSINFO[2] The patch level.

BASH_VERSINFO[3] The build version.

BASH_VERSINFO[4] The release status (e.g., beta1).

BASH_VERSINFO[5] The value of MACHTYPE.

Some of the variables like hostname, pwd does work though

4

You are typing the variable directly and the shell then interprets what you have entered as a command and searches the entries in your $PATH variable for a command of the same name. Since you have no entries in your $PATH with the name BASH and BASH_VERSINFO, you received the "command not found" error message. pwd and hostname are, on the other hand, valid commands, as you have discovered.

You must instead issue a command for printing variables (generally echo or printf, both shell built-ins), and provide the variable you wish to inspect as an argument of the command. Note that command names are often composed entirely of lowercase letters while shell environment variables are often composed entirely of uppercase letters.

To display the values of the variables you have mentioned, try something like the following:

$ echo "$BASH"
/bin/bash
$ for i in {0..4}; do printf "%d\t%s\n" "$i" "${BASH_VERSINFO[$i]}"; done
0   4
1   4
2   12
3   1
4   release

To print array variables, you use the syntax "${ARR_NAME[$index]}." Bash indices are 0-based. In general, you should encapsulate variables (including arrays) in double quotes to avoid unwanted behavior (omission of quotes is equivalent to the split-glob operator, as explained here).

  • +1 for explaining the ${ []} construct to the OP. – Rui F Ribeiro Sep 6 '18 at 1:24
1
pwd

is a command, as is

echo $BASH

where as

BASH

is not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.