Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH shell parameter (indirect) expansion

Tags:

bash

shell

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.

  1. Can I match a string at the end of the variable name?

    But mostly

  2. Can I match a string anywhere in the variable name?

    Oh and what would be delightful

  3. 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)

like image 988
yPhil Avatar asked May 12 '11 11:05

yPhil


People also ask

What is $@ in bash?

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.

What is $$ in shell script?

$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing. 8. $!

What is $$ variable in bash?

$# - 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.

What does shell expansion mean?

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.


1 Answers

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.

like image 83
Lesmana Avatar answered Sep 28 '22 15:09

Lesmana