how can i do echo this way ?

interpretive language scripts


Moderator: Forum moderators

Post Reply
User avatar
AntonioPt
Posts: 306
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 130 times
Been thanked: 55 times

how can i do echo this way ?

Post by AntonioPt »

hello all i need some help :)
how do i achive this ? thxx in advance

Code: Select all

# let make 2 var
var_1="this is var 1"
var_2="this is var 2"
# now i wanna call them in loop
for i in {1..2} ; do
  echo "var_$i"
done

# i want this output
this is var 1
this is var 2

Why astronauts use Linux
Because you can't open windows in space

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

Re: how can i do echo this way ?

Post by rcrsn51 »

Code: Select all

eval echo \$var_$i
User avatar
AntonioPt
Posts: 306
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 130 times
Been thanked: 55 times

Re: how can i do echo this way ?

Post by AntonioPt »

rcrsn51 wrote: Sun Feb 09, 2025 8:30 pm

Code: Select all

eval echo \$var_$i

thxxxxxxxxxxx

Why astronauts use Linux
Because you can't open windows in space

mow9902
Posts: 205
Joined: Fri Jul 24, 2020 11:57 pm
Has thanked: 17 times
Been thanked: 69 times

Re: how can i do echo this way ?

Post by mow9902 »

AntonioPt wrote: Sun Feb 09, 2025 7:39 pm

hello all i need some help :)
how do i achive this ? thxx in advance

Code: Select all

# let make 2 var
var_1="this is var 1"
var_2="this is var 2"
# now i wanna call them in loop
for i in {1..2} ; do
  echo "var_$i"
done

# i want this output
this is var 1
this is var 2

change echo "var_$i" to
echo "This is var_$i"

User avatar
AntonioPt
Posts: 306
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 130 times
Been thanked: 55 times

Re: how can i do echo this way ?

Post by AntonioPt »

mow9902 wrote: Sun Feb 09, 2025 10:46 pm
AntonioPt wrote: Sun Feb 09, 2025 7:39 pm

hello all i need some help :)
how do i achive this ? thxx in advance

Code: Select all

# let make 2 var
var_1="this is var 1"
var_2="this is var 2"
# now i wanna call them in loop
for i in {1..2} ; do
  echo "var_$i"
done

# i want this output
this is var 1
this is var 2

change echo "var_$i" to
echo "This is var_$i"

Thank you but that wasn't what i wanna

Why astronauts use Linux
Because you can't open windows in space

User avatar
MochiMoppel
Posts: 1343
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 521 times

Re: how can i do echo this way ?

Post by MochiMoppel »

Another way:

Code: Select all

var_1="this is var 1"
var_2="this is var 2"
for i in {1..2} ; do
  i=var_$i
  echo "${!i}"
done
User avatar
AntonioPt
Posts: 306
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 130 times
Been thanked: 55 times

Re: how can i do echo this way ?

Post by AntonioPt »

MochiMoppel wrote: Mon Feb 10, 2025 2:32 am

Another way:

Code: Select all

var_1="this is var 1"
var_2="this is var 2"
for i in {1..2} ; do
  i=var_$i
  echo "${!i}"
done

I like that :) thxx

Why astronauts use Linux
Because you can't open windows in space

User avatar
MochiMoppel
Posts: 1343
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 521 times

Re: how can i do echo this way ?

Post by MochiMoppel »

AntonioPt wrote: Sun Feb 09, 2025 9:11 pm
rcrsn51 wrote: Sun Feb 09, 2025 8:30 pm

Code: Select all

eval echo \$var_$i

thxxxxxxxxxxx

This only appears to work because your example values contain only single spaces.
Will not work with other or with continuous whitespace

Code: Select all

var_1='this   is   var 1'
var_2='this		is     var 2'
for i in {1..2} ; do
  eval echo \$var_$i
done
User avatar
AntonioPt
Posts: 306
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 130 times
Been thanked: 55 times

Re: how can i do echo this way ?

Post by AntonioPt »

MochiMoppel wrote: Mon Feb 10, 2025 3:27 am
AntonioPt wrote: Sun Feb 09, 2025 9:11 pm
rcrsn51 wrote: Sun Feb 09, 2025 8:30 pm

Code: Select all

eval echo \$var_$i

thxxxxxxxxxxx

This only appears to work because your example values contain only single spaces.
Will not work with other or with continuous whitespace

Code: Select all

var_1='this   is   var 1'
var_2='this		is     var 2'
for i in {1..2} ; do
  eval echo \$var_$i
done

ahhhhh thankyou for let me know about this small detail very important indeed just update with your code more clean

Why astronauts use Linux
Because you can't open windows in space

User avatar
rcrsn51
Posts: 1486
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 424 times

Re: how can i do echo this way ?

Post by rcrsn51 »

MochiMoppel wrote: Mon Feb 10, 2025 3:27 am

This only appears to work because your example values contain only single spaces.
Will not work with other or with continuous whitespace

Code: Select all

eval echo \"\$var_$i\"
superhik
Posts: 63
Joined: Mon Jun 19, 2023 7:56 pm
Has thanked: 6 times
Been thanked: 26 times

Re: how can i do echo this way ?

Post by superhik »

Instead of eval, printf, IFS and cat are options that work.

Code: Select all

var_1='this   is   var 1'
var_2='this		is     var 2'  # Contains tabs and multiple spaces

for i in {1..2}; do
  var_name="var_$i"
  printf "%s\n" "${!var_name}"  # Preserves all spaces and tabs
done

for i in {1..2}; do
  var_name="var_$i"
  IFS= read -r value <<< "${!var_name}"  # Read the variable while preserving spaces
  echo "$value"
done

for i in {1..2}; do
  var_name="var_$i"
  cat <<< "${!var_name}"
done

Also echo -E works in bash

Code: Select all

for i in {1..2}; do
  var_name="var_$i"
  echo -E "${!var_name}"
done

Best universal options: printf "%s\n" or IFS= read -r

User avatar
MochiMoppel
Posts: 1343
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 521 times

Re: how can i do echo this way ?

Post by MochiMoppel »

superhik wrote: Tue Feb 25, 2025 9:41 pm

Also echo -E works in bash

This was already one of the proposed solutions - withouth the explicit -E option. As this is the default in echo anyway and therefore not required, do you see any scenario where the -E option makes a difference?

Best universal options: printf "%s\n" or IFS= read -r

What makes them "best"? They also work only with bash.

superhik
Posts: 63
Joined: Mon Jun 19, 2023 7:56 pm
Has thanked: 6 times
Been thanked: 26 times

Re: how can i do echo this way ?

Post by superhik »

Actually, IFS and printf are not Bash-specific, they both work in POSIX compliant shells.
Unlike echo, which may collapse spaces and interpret special characters differently across systems, printf always prints exactly what you pass to it.
printf "%s\n" always preserves spaces/tabs → Works in sh, bash, dash, ksh, etc.
The default read behavior trims spaces, but setting IFS='' prevents this.

Code: Select all

var='   hello   world   '
IFS= read -r value <<EOF
$var
EOF
printf "%s\n" "$value"  # Preserves spaces exactly

IFS works in all POSIX shells, including sh, dash, ksh, etc.
cat <<< is bash only.
eval "echo \${var}" Might collapse, and if var contains malicious code (e.g., var='$(rm -rf /)'), eval will execute it!
eval "echo \"\${var}\"" preserves spaces, but still risky.
printf "%s\n" "$var" preserves spaces and it's safe. Therefore it's the best universal solution.

MochiMoppel wrote: Wed Feb 26, 2025 2:58 am
superhik wrote: Tue Feb 25, 2025 9:41 pm

Also echo -E works in bash

This was already one of the proposed solutions - withouth the explicit -E option. As this is the default in echo anyway and therefore not required, do you see any scenario where the -E option makes a difference?

You're absolutely right! In modern Bash, echo -E is redundant because -E is the default behavior.
However, there are a few scenarios where echo -E can make a difference:
In POSIX sh, echo behavior varies by implementation. Some shells (like dash or BusyBox sh) might still interpret escape sequences by default, so using -E explicitly in Bash ensures consistency when running scripts across different shells.
In Bash, enabling shopt -s xpg_echo makes echo behave like echo -e, where \n and other escapes are interpreted by default.
Using echo -E here prevents this behavior.
If a user has an alias or function overriding echo in their shell profile (~/.bashrc, ~/.profile), echo might behave differently.
alias echo='echo -e'
Now, echo "Hello\nWorld" would always interpret \n as a newline, even in Bash.
Using echo -E forces literal output, ignoring the alias.

User avatar
MochiMoppel
Posts: 1343
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 521 times

Re: how can i do echo this way ?

Post by MochiMoppel »

superhik wrote: Wed Feb 26, 2025 10:32 am

Actually, IFS and printf are not Bash-specific, they both work in POSIX compliant shells.

Sure. I was rather referring to the whole constructs in which they are used. These constructs are Bash-specific, using "indirect expansion" (${!var_name}). Which may, after some changes to the loop, leave the eval evil as the only POSIX compliant solution :twisted:

Post Reply

Return to “Scripts”