Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of variables whose name matches a certain pattern

In bash

echo ${!X*} 

will print all the names of the variables whose name starts with 'X'.
Is it possible to get the same with an arbitrary pattern, e.g. get all the names of the variables whose name contains an 'X' in any position?

like image 607
Paolo Tedesco Avatar asked Feb 04 '09 14:02

Paolo Tedesco


2 Answers

Use the builtin command compgen:

compgen -A variable | grep X 
like image 78
Johannes Schaub - litb Avatar answered Oct 05 '22 13:10

Johannes Schaub - litb


This should do it:

env | grep ".*X.*" 

Edit: sorry, that looks for X in the value too. This version only looks for X in the var name

env | awk -F "=" '{print $1}' | grep ".*X.*" 

As Paul points out in the comments, if you're looking for local variables too, env needs to be replaced with set:

set | awk -F "=" '{print $1}' | grep ".*X.*" 
like image 29
diciu Avatar answered Oct 05 '22 12:10

diciu