for example, what's the difference between '/usr/bin/perl' and 'env
perl' ?
I know env may set a environment variable in system, so my question also includes:
1. where to see a shell environment variable? I tried 'echo $ENV'
showing nothing.
the key for perl is "_" in environment variable? under this key, why
'env perl' just works?
I am not sure how 'env' command works.
for example, what's the difference between '/usr/bin/perl' and 'env perl' ?
I know env may set a environment variable in system,
so my question also includes:
1. where to see a shell environment variable? I tried 'echo $ENV'
showing nothing.
why 'env perl' just works?
$ perl -le 'for( keys %ENV ){print "$_ --> $ENV{$_}"}' |grep perl
_ --> /usr/bin/perl
the key for perl is "_" in environment variable? under this key, why
'env perl' just works?
On 2024-07-19, [email protected] wrote:
$ perl -le 'for( keys %ENV ){print "$_ --> $ENV{$_}"}' |grep perl
_ --> /usr/bin/perl
the key for perl is "_" in environment variable? under this key, why
'env perl' just works?
Perl $_ is the current (unnamed) value of your loop "for". You could
write it like this:
foreach my $key (keys %ENV) { print "$key=$ENV{$key}" }
https://perldoc.perl.org/variables
$ VAR1=foo && ./a.sh
$ export VAR2=foo; ./a.sh
$ ./b.sh
$VAR1 will be seen by a.sh only, but $VAR2 can be seen my current login session (such as b.sh). Am I right? I am a bit confused about env scope.
In addition to what everyone else has said about env(1), there is the
fact that Korn derived shells also supports some of the same features.
env VAR1=foo VAR2=bar random-command
VAR1=foo VAR2=bar random-command
On Sat, Jul 20, 2024 at 05:46:23 +0800, [email protected] wrote:
$ VAR1=foo && ./a.sh
$ export VAR2=foo; ./a.sh
$ ./b.sh
$VAR1 will be seen by a.sh only, but $VAR2 can be seen my current
login
session (such as b.sh). Am I right? I am a bit confused about env
scope.
If we assume NO other commands have been executed in this shell so far,
then:
VAR1 is not marked for export. It's just a regular shell variable.
It won't be seen by either call to ./a.sh which is a (non-subshell)
child process, not will it be seen by ./b.sh which is also a
non-subshell
child.
VAR2 is marked for export by the interactive shell. It's a permanent
part of the shell's environment and will be seen by all child processes
from that point forward.
VAR2 will therefore be seen by the second call to ./a.sh and the call
to ./b.sh.
Now, what you didn't ask, but what I *expected* you to ask, is:
What's the difference between these two commands?
VAR3=foo ./a.sh
VAR3=bar; ./a.sh
In the first command, VAR3 is placed in the environment of the command
being executed. ./a.sh will see it. VAR3 will not survive beyond
this command. It will be discarded, and future commands will not be
aware it ever existed.
In the second command, VAR3 is created as a regular variable in the
current shell, but not exported to the environment. It will NOT be
seen by ./a.sh, but it WILL be seen by future shell commands within
this session.
$ VAR=foo ./a.sh
i can see VAR=foo
What's the difference between these two commands?
VAR3=foo ./a.sh
VAR3=bar; ./a.sh
In the first command, VAR3 is placed in the environment of the command being executed. ./a.sh will see it. VAR3 will not survive beyond
this command. It will be discarded, and future commands will not be
aware it ever existed.
In the second command, VAR3 is created as a regular variable in the
current shell, but not exported to the environment. It will NOT be
seen by ./a.sh, but it WILL be seen by future shell commands within
this session.
I can not clearly understand for this statement. what's "future shell commands"? can you show an example?
I can not clearly understand for this statement. what's "future shell
commands"? can you show an example?
hobbit:~$ unset -v VAR
hobbit:~$ VAR=bar; ./a.sh
I am a.sh, and inside me, VAR=<>.
hobbit:~$ echo "VAR=<$VAR>"
VAR=<bar>
On Sat, Jul 20, 2024 at 06:30:42 +0800, [email protected] wrote:
On 2024-07-20 06:25, Greg Wooledge wrote:
I can not clearly understand for this statement. what's "future shell
commands"? can you show an example?
hobbit:~$ unset -v VAR
hobbit:~$ VAR=bar; ./a.sh
I am a.sh, and inside me, VAR=<>.
hobbit:~$ echo "VAR=<$VAR>"
VAR=<bar>
OK I know that. $VAR can be seen by future shell command in this
session, but cannot be seen by a sub-shell such as a.sh.
a.sh is NOT a subshell. That's really super important.
a.sh is a NON-subshell child process.
In an actual subshell, you *would* see the variable, because subshells inherit regular variables as well as environment variables.
hobbit:~$ unset -v VAR
hobbit:~$ VAR=foo; (echo "I am a subshell, and VAR=<$VAR>")
I am a subshell, and VAR=<foo>
hobbit:~$ ./a.sh
I am a.sh, and inside me, VAR=<>.
https://mywiki.wooledge.org/SubShell
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 155:44:54 |
| Calls: | 12,092 |
| Files: | 15,000 |
| Messages: | 6,517,709 |