When I enter echo ${!BASH*}
in my shell, I get all env vars whose name start with the string "BASH":
BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND BASH_LINENO BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION
The BASH man page is quite discreet about this feature called shell parameter (indirect) expansion. This info (the syntax of this particular globbing mode) must be somewhere but I can't find it.
Can I match a string at the end of the variable name?
But mostly
Can I match a string anywhere in the variable name?
Oh and what would be delightful
Can I match anything (*) anywhere in the variable name? (basically list any variable inside a for loop, and BTW this exactly what I'm trying to do)
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing. 8. $!
$# - How many arguments were passed to the Bash script. $@ - All the arguments supplied to the Bash script. $? - The exit status of the most recently run process. $$ - The process ID of the current script. $USER - The username of the user running the script.
Definition of shell expansion : a drawing showing the shell plating of a ship and giving the size, shape, and weight of the plates and their connections.
As far as I know there is no builtin way to do indirect name expansion with matching at end of name.
Here is a command with which you can do what you want:
$ compgen -v | grep -E "HOME$"
This will match every variable which name ends in HOME.
By varying the grep part you can match any part of the variable name.
compgen
is a bash builtin meant to be used in completion scripts. compgen -v
will list all variable names. compgen -v BASH
will list the same variables as echo ${!BASH*}
, but one per line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With