Home > database >  list all the variables in a Bash script with a certain pattern
list all the variables in a Bash script with a certain pattern

Time:08-21

I want to list all the variables in a Bash script with a certain pattern.

For example:

repo_website_branch
repo_website_gitUrl
repo_playground_branch
repo_playground_gitUrl

When I try:

echo "${!repo_@}"

I get the following output in the console:

repo_website_branch repo_website_gitUrl repo_playground_branch repo_playground_gitUrl

But when I try:

echo "${!repo_*_gitUrl@}"

Expected result:

repo_website_gitUrl repo_playground_gitUrl

Current result:
Nothing is output in the console.

CodePudding user response:

The * doesn't act as a special pattern character in this kind of parameter expansion. You may try

for v in "${!repo_@}"; do [[ $v = *_gitUrl ]] && echo "$v"; done

instead.

  •  Tags:  
  • bash
  • Related