开发者

BASH shell parameter (indirect) expansion

开发者 https://www.devze.com 2023-03-05 15:16 出处:网络
When I enter echo ${!BASH*} in my shell, I get all env vars whose name start with the string "BASH":

When I enter echo ${!BASH*} in my shell, I get all env vars whose name start with the string "BASH":

开发者_Go百科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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消