0

There is a.sh:

#!/bin/bash

foo=':bar baz:'
declare varvar=aaaaaa
export xyz=abc

echo "$foo ok"
hoge=huga env piyo=alice bash -i

Run ./a.sh (| is cursor, > is prompt):

:bar baz: ok
> |

There is the input:

echo "foo: '$foo'"
echo "varvar: '$varvar'"
echo "xyz: '$xyz'"
echo "hoge: '$hoge'"
echo "piyo: '$piyo'"

Expected output:

> echo "foo: '$foo'"
foo: ':bar baz:'
> echo "varvar: '$varvar'"
varvar: 'aaaaaa'
> echo "xyz: '$xyz'"
xyz: 'abc'
> echo "hoge: '$hoge'"
hoge: 'huga'
> echo "piyo: '$piyo'"
piyo: 'alice'
> |

Actual output:

> echo "foo: '$foo'"
foo: ''
> echo "varvar: '$varvar'"
varvar: ''
> echo "xyz: '$xyz'"
xyz: 'abc'
> echo "hoge: '$hoge'"
hoge: 'huga'
> echo "piyo: '$piyo'"
piyo: 'alice'
> |

How do I get $foo and $varvar to display correctly?

sakkke
  • 183
  • 6
  • 1
    Export them? You're already doing that with `xyz`. Or make the assignment as with `hoge` or `piyo`. It's unclear what the question is as you have the answer in your code. – Kusalananda Feb 16 '22 at 07:12
  • Do I need to export the variables to display them? Is there any way to display them without exporting? I would like to use it for debugging. – sakkke Feb 16 '22 at 07:17
  • Do you know how environment variables work? Child processes inherit environment variables. Other variables are not inherited. You create environment variables for a child process to see in the various ways you show, with `export`, by assigning them at the start of a command's command line or by setting them in the arguments to `env` when using that tool to launch the command. – Kusalananda Feb 16 '22 at 07:29
  • It gave me an understanding of environment variables. Thank you. – sakkke Feb 16 '22 at 09:19

1 Answers1

0

Fix a.sh:

#!/bin/bash

set -a
foo=':bar baz:'
declare varvar=aaaaaa
export xyz=abc

echo "$foo ok"
hoge=huga env piyo=alice bash -i

It lets all variables export.

sakkke
  • 183
  • 6