what is the significance of exporting path in shell script? - shell

I have seen below two lines in a shell script.
Im new to unix scripting, what is the use of setting this?
PATH=$PATH:/bin:/usr/bin:/usr/sbin:/sbin:/etc:/usr/ucb:/usr/ccs/bin:/usr/local/bin
export PATH
Thanks in advance

If you export something (in bash anyway which I assume is your shell), it will mark that something to be available in subsequently executed commands.
$ FOO=1 # Set the variable
$ echo $FOO # Check the value
1
$ bash # New shell here.
$ echo $FOO # No value since it's not exported
$ exit # Quit the subshell
$ export FOO # Export it
$ bash
$ echo $FOO # It has a value now
1
export is a shell builtin for bash so doing a help export will give you more information on it.

Explicitly exporting the PATH doesn't hurt but generally has no effect as the PATH variable is almost certainly already marked as exported when you launch a shell script.

Related

Assigning a variable in a shell script for use outside of the script

I have a shell script that sets a variable. I can access it inside the script, but I can't outside of it. Is it possible to make the variable global?
Accessing the variable before it's created returns nothing, as expected:
$ echo $mac
$
Creating the script to create the variable:
#!/bin/bash
mac=$(cat \/sys\/class\/net\/eth0\/address)
echo $mac
exit 0
Running the script gives the current mac address, as expected:
$ ./mac.sh
12:34:56:ab:cd:ef
$
Accessing the variable after its created returns nothing, NOT expected:
$ echo $mac
$
Is there a way I can access this variable at the command line and in other scripts?
A child process can't affect the parent process like that.
You have to use the . (dot) command — or, if you like C shell notations, the source command — to read the script (hence . script or source script):
. ./mac.sh
source ./mac.sh
Or you generate the assignment on standard output and use eval $(script) to set the variable:
$ cat mac.sh
#!/bin/bash
echo mac=$(cat /sys/class/net/eth0/address)
$ bash mac.sh
mac=12:34:56:ab:cd:ef
$ eval $(bash mac.sh)
$ echo $mac
12:34:56:ab:cd:ef
$
Note that if you use no slashes in specifying the script for the dot or source command, then the shell searches for the script in the directories listed in $PATH. The script does not have to be executable; readable is sufficient (and being read-only is beneficial in that you can't run the script accidentally).
It's not clear what all the backslashes in the pathname were supposed to do other than confuse; they're unnecessary.
See ssh-agent for precedent in generating a script like that.

Expanding a shell variable in a script

If I define my archive folder it in my environment and export it, how do I access it in a shell script and run a program?
ARCHIVE=/home/kschmidt/public_html/CS265/Assignments/DrMath/Archive
export ARCHIVE
./prob1
Currently, my prob1 contains this code which I get an error when I try to run.
#!/bin/bash
print ARCHIVE
You expand a shell variable by prefixing it with a $, and print it with echo or printf command - shell doesn't have a print command:
echo "$ARCHIVE"
or
printf '%s\n' "$ARCHIVE"
As an aside, it is not good to use relative paths (as in ./prob1) in a script, unless you are explicitly cding to the directory where prob1 exists. So, either:
do an explicit cd to the script directory before invoking it with a relative path
or
use an absolute path (as in /path/to/prob1)
Related:
Why is printf better than echo - on Unix & Linux Stack Exchange
Shell Parameter Expansion - GNU Bash Manual
When to wrap quotes around a shell variable?
prob1's code should be like:
#!/bin/bash
bash $ARCHIVE
then prob1 run like this:
bash prob1
I hope this help.

How to export environment variable set in perl script to batch shell?

I am executing a perl script on windows, using a batch script.
I am setting below variable in batch script:
SET PATH_VAR=C:\Users\
I am able to access PATH_VAR in perl as below:
my $path1 = $ENV{'PATH_VAR'}
I would like to also export environment variables set in perl to batch. Like the inverse of what I am doing now.
Is there a way to do that?
PS:
I tried this, but it doesn't work:
$ENV{'PATH_Z'}="Hello World";
Changes to environment variables can not effect the parent process, it's part of how they work, so nothing you do in the Perl script can change the environment variables of the batch script. However any child process, started with exec(), system() or `` will see the changes you made in the Perl script.
The only way to do this is to have the Perl script output shell statements, and for the shell to evaluate the output.
Bash example:
$ export FOO=123
$ echo $FOO
123
$ perl -e 'print "export FOO=456\n"' ; echo $FOO
123
$ $(perl -e 'print "export FOO=789\n"') ; echo $FOO
789
Edit: I see OP is using Windows, so this answer doesn't apply :-(

What does "export" do in shell programming? [duplicate]

This question already has answers here:
Defining a variable with or without export
(15 answers)
Closed 3 years ago.
As far as I can tell, variable assignment is the same whether it is or is not preceded by "export". What's it for?
Exported variables such as $HOME and $PATH are available to (inherited by) other programs run by the shell that exports them (and the programs run by those other programs, and so on) as environment variables. Regular (non-exported) variables are not available to other programs.
$ env | grep '^variable='
$ # No environment variable called variable
$ variable=Hello # Create local (non-exported) variable with value
$ env | grep '^variable='
$ # Still no environment variable called variable
$ export variable # Mark variable for export to child processes
$ env | grep '^variable='
variable=Hello
$
$ export other_variable=Goodbye # create and initialize exported variable
$ env | grep '^other_variable='
other_variable=Goodbye
$
For more information, see the entry for the export builtin in the GNU Bash manual, and also the sections on command execution environment and environment.
Note that non-exported variables will be available to subshells run via ( ... ) and similar notations because those subshells are direct clones of the main shell:
$ othervar=present
$ (echo $othervar; echo $variable; variable=elephant; echo $variable)
present
Hello
elephant
$ echo $variable
Hello
$
The subshell can change its own copy of any variable, exported or not, and may affect the values seen by the processes it runs, but the subshell's changes cannot affect the variable in the parent shell, of course.
Some information about subshells can be found under command grouping and command execution environment in the Bash manual.
it makes the assignment visible to subprocesses.
$ foo=bar
$ bash -c 'echo $foo'
$ export foo
$ bash -c 'echo $foo'
bar
Well, it generally depends on the shell. For bash, it marks the variable as "exportable" meaning that it will show up in the environment for any child processes you run.
Non-exported variables are only visible from the current process (the shell).
From the bash man page:
export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the environment of subsequently executed commands.
If the -f option is given, the names refer to functions. If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed.
The -n option causes the export property to be removed from each name.
If a variable name is followed by =word, the value of the variable is set to word.
export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a function.
You can also set variables as exportable with the typeset command and automatically mark all future variable creations or modifications as such, with set -a.

Tcsh and Bash Initialization

I would like to be able to source a file to set up some environment variables, but do it so it's independent of the shell being used.
For example
%: source START.env
# START.env
if [ $SHELL == "bash" ]; then
source START.env.bash # sets environment variables
else
source START.env.tcsh # sets environment variables
fi
However, this code will only work for bash. How can one make it compatible with both bash and tcsh?
I have to source the file because I want the environment variables to stick afterwards.
Create your variable file for tcsh:
setenv FOO 42
setenv BAR baz
setenv BAZ "foo bar"
In your tcsh script use:
source filename
In your Bash script use:
while read -r cmd var val
do
if [[ $cmd == "setenv" ]]
then
declare -x "$var=$val"
fi
done < filename
It would be somewhat difficult to create a script that would run on both tcsh and Bash.
You could do what Dennis says. Or I think this is slightly more eloquent
(in bash)
sed -i 's/setenv \([a-zA-Z0-9_]*\) /declare -x \1=/g' filename
source filename
(in tcsh script)
sed -i 's/declare -x \([a-zA-Z0-9_]*\)=/setenv \1 /g' filename
source filename
(Note this will actually change your sourced file to the appropriate syntax each time it's run.)
So I ended up going with a full on DSL approach to solve this (see clamshell).

Resources