0

I am curious to know that whether it is possible in bash that we can run for loop on a bunch of variables and call those values within for loop. Example:

a="hello"
b="world"
c="this is bash"

for f in a b c; do {
  echo $( $f )
OR
  echo $ ( "$f" )
} done

I know this is not working but can we call the values saved in a, b and c variables in for loop with printing f. I tried multiple way but unable to resolve.

5
  • 1
    Instead for f in $a $b $c and echo $f Commented Sep 1, 2021 at 17:44
  • Thank you @JNevil. It's so easy. Unnecessary, I was trying hard. Commented Sep 1, 2021 at 17:54
  • 1
    to avoid splitting on white space make sure you double quote your variable references, eg, for f in "$a" "$b" "$c" and echo "$f" Commented Sep 1, 2021 at 17:54
  • 2
    what do you mean by "call those values"? execute the command named by the value of the variable? Commented Sep 1, 2021 at 17:54
  • @PiyushkumarPatel : What does it mean to call a variable, respectively to call a value? Commented Sep 2, 2021 at 7:04

3 Answers 3

3

You need the ! like this:

for f in a b c; do
  echo "${!f}"
done
Sign up to request clarification or add additional context in comments.

2 Comments

Now I'm confused as to whether the OP wanted a direct or an indirect variable. Well, it's educational either way...
@agc : Actually I find the question confusing. I have no idea what the OP wants to achieve.
2

You can also use a nameref:

#!/usr/bin/env bash

a="hello"
b="world"
c="this is bash"

declare -n f
for f in a b c; do 
  printf "%s\n" "$f"
done

From the documentation:

If the control variable in a for loop has the nameref attribute, the list of words can be a list of shell variables, and a name reference will be established for each word in the list, in turn, when the loop is executed.

Comments

0

Notes on the OP's code, (scroll to bottom for corrected version):

for f in a b c; do {
  echo $( $f )
} done

Problems:

  1. The purpose of { & } is usually to put the separate outputs of separate unpiped commands into one stream. Example of separate commands:

    echo foo; echo bar | tac
    

    Output:

    foo
    bar
    

    The tac command puts lines of input in reverse order, but in the code above it only gets one line, so there's nothing to reverse. But with curly braces:

    { echo foo; echo bar; } | tac
    

    Output:

    bar
    foo
    

    A do ... done already acts just like curly braces.
    So "do {" instead of a "do" is unnecessary and redundant; but it won't harm anything, or have any effect.

  2. If f=hello and we write:

    echo $f
    

    The output will be:

    hello
    

    But the code $( $f ) runs a subshell on $f which only works if $f is a command. So:

    echo $( $f )
    

    ...tries to run the command hello, but there probably is no such command, so the subshell will output to standard error:

    hello: command not found
    

    ...but no data is sent to standard output, so echo will print nothing.


To fix:

a="hello"
b="world"
c="this is bash"

for f in "$a" "$b" "$c"; do
  echo "$f"
done

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.