2

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.

1 Answer 1

4

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

or

compgen -v -X '!repo_*_gitUrl'

instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.