<div dir="ltr"><div>I use #!/bin/bash explicitly because I tend to use bash-isms in my scripts and I&#39;d rather not have it fail part way because I&#39;m using a strictly POSIX interpreter on accident. I&#39;d prefer it to not run at all if bash isn&#39;t present. If you are diligent about using only POSIX features, then yes, using #!/bin/sh is a better choice.<br>

<br></div>QH<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 25, 2013 at 9:04 AM, Stephen Queen <span dir="ltr">&lt;<a href="mailto:svqueen@gmail.com" target="_blank">svqueen@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div><div><div>When sourcing a file #!/bin/bash has no effect. It looks to me as if it is seen as a comment. For fun try this:<br>

</div><div>cat my_fun<br></div>#!/bin/foo<br>
</div>export BAR=Hello<br><br></div>Now source it<br></div>. my_fun<br></div>No error messages. Now <br></div>$ echo $BAR<br></div>Hello<br><br></div>So you can add #!/bin/bash, to a file you are going to source, but it has no effect.<br>


</div>Though if you are always going to add the interpreter to the first line #!/bin/sh is a better choice. It is more portable. It is usually a link to a default shell, whereas bash is not necessarily installed on all systems. A lot of embedded systems don&#39;t use bash. <br>


<br><br></div><div>A good example of a sourced file on a debian system would be /lib/init/vars.sh. It is sourced by a good number of the start up scripts.<br><br></div><div>Steve<br></div></div><div class="HOEnZb"><div class="h5">

<div class="gmail_extra"><br>
<br><div class="gmail_quote">On Mon, Jun 24, 2013 at 5:03 PM, Quentin Hartman <span dir="ltr">&lt;<a href="mailto:qhartman@gmail.com" target="_blank">qhartman@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div dir="ltr"><div><div>Just a note that removing the #! statement at the top of the file is usually a bad idea. That doesn&#39;t directly start a new shell, that tells the system (not sure if it&#39;s bash itself, or if it&#39;s a kernel thing) which interpreter to use for the script. You can get weird results if you leave that out and make the script executable.<br>




<br></div>It does though create a new subshell as was mentioned, which always happens with script execution (having nothing to do with #!), which is why those env_vars don&#39;t show up in the parent shell. In order to get them to show up in the parent shell, you have to source them from the parent shell:<br>




<br>04:57 PM:qhartman@qhstation$ cat foo.sh<br>#!/bin/bash<br>export FOO=bar<br><br>CWD:~<br>04:57 PM:qhartman@qhstation$ ./foo.sh <br><br>CWD:~<br>04:57 PM:qhartman@qhstation$ echo $FOO<br><br><br>CWD:~<br>04:57 PM:qhartman@qhstation$ source foo.sh<br>




<br>CWD:~<br>04:58 PM:qhartman@qhstation$ echo $FOO<br>bar<br><br><br></div>If you need to have those vars set outside of the context of the script you are running, you need to add or modify those vars in the appropriate user&#39;s login shell config (usually .bashrc), and then the next time that user invokes a shell they will be set how you want.<br>




</div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 24, 2013 at 3:28 PM, Mark G. Harvey <span dir="ltr">&lt;<a href="mailto:markgharvey@yahoo.com" target="_blank">markgharvey@yahoo.com</a>&gt;</span> wrote:<br>




<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-size:10pt;font-family:Courier New,courier,monaco,monospace,sans-serif"><div><span style="font-size:13px"><br>




</span></div><div style="font-style:normal;background-color:transparent"><span style="font-size:small"><br></span></div><div style="font-style:normal;background-color:transparent"><span style="font-size:13px">so removing #!/bin/bash/ at the top of the script to stay within the same shell session doesn&#39;t make any detectable difference</span></div>




<div style="font-style:normal;background-color:transparent"><span style="font-size:13px"><br></span></div><div style="font-style:normal;background-color:transparent"><span style="font-size:13px"><span>I also added sourcing the file where the variables are set ( # </span><span>source </span><span>/etc/profile.d/00_jdk.sh
  ) </span><span style="background-color:transparent"> </span></span></div><div style="font-style:normal;background-color:transparent"><span style="background-color:transparent;font-size:13px"><br></span></div><div style="font-style:normal;background-color:transparent">




<span style="background-color:transparent;font-size:13px">the variables PATH and JAVA_HOME show up correct when the script is running ... but when finished, the variables are still not changed</span></div><div style="font-style:normal;background-color:transparent">




<span style="background-color:transparent;font-size:13px"><br></span></div><div style="background-color:transparent"><span style="font-size:13px"><div><div style="background-color:transparent">[root@87148-mondev01 ~]# echo $PATH</div>




</div><div style="background-color:transparent">/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin  ... should be appended with <span style="background-color:transparent">/usr/java/jdk1.7.0_21/bin</span></div>




<div><div style="background-color:transparent">[root@87148-mondev01 ~]# echo $JAVA_HOME</div></div><div style="font-style:normal;background-color:transparent">... should have an entry <span style="background-color:transparent">/usr/java/jdk1.7.0_21/bin ... but it is blank </span></div>




</span></div><div style="font-style:normal;background-color:transparent"><span style="font-size:13px"><br></span></div><div style="font-style:normal;background-color:transparent"><span style="font-size:13px"><br></span></div>




<div style="font-style:normal;background-color:transparent"><span style="font-size:small"><br></span></div><div><br><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;margin-top:5px;padding-left:5px">




  <div style="font-family:&#39;Courier New&#39;,courier,monaco,monospace,sans-serif;font-size:10pt"> <div style="font-family:&#39;times new roman&#39;,&#39;new york&#39;,times,serif;font-size:12pt"> <div dir="ltr"> <hr size="1">




  <font face="Arial"> <b><span style="font-weight:bold">From:</span></b> &quot;<a href="mailto:dennisjperkins@comcast.net" target="_blank">dennisjperkins@comcast.net</a>&quot; &lt;<a href="mailto:dennisjperkins@comcast.net" target="_blank">dennisjperkins@comcast.net</a>&gt;<br>




 <b><span style="font-weight:bold">To:</span></b> CLUE&#39;s mailing list &lt;<a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a>&gt; <br> <b><span style="font-weight:bold">Sent:</span></b> Monday, June 24, 2013 2:56 PM<div>




<div><br> <b><span style="font-weight:bold">Subject:</span></b> Re: [clue] env variables<br> </div></div></font> </div><div><div> <div><br><div><div><div style="font-size:12pt;font-family:Arial">The proper place for these might be in one of the Bash config files.<br>




<br><hr><b>From: </b>&quot;Stephen Queen&quot; &lt;<a href="mailto:svqueen@gmail.com" target="_blank">svqueen@gmail.com</a>&gt;<br><b>To: </b>&quot;Mark G. Harvey&quot; &lt;<a href="mailto:markgharvey@yahoo.com" target="_blank">markgharvey@yahoo.com</a>&gt;, &quot;CLUE&#39;s mailing list&quot; &lt;<a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a>&gt;<br>




<b>Sent: </b>Monday, June 24, 2013 2:37:49 PM<br><b>Subject: </b>Re: [clue] env variables<br><br><div dir="ltr"><div><div><div><div><div><div>When you add <br>#!/bin/bash<br></div>to the top of your script, you are starting a new shell. When the script completes, it exits that shell. Instead &quot;source&quot; you environmental variables. Create a file my_env that contains your export commands<br>





# cat my_env<br>export JAVA_HOME=/usr/java/jdk1.7.0_21<br>export PATH=$PATH:/usr/java/jdk1.7.0_21/bin<br><br></div>Then source the file <br></div>. my_env<br></div><div>(space between . and my_env, does not need to be executable).<br>





</div>Now when you<br></div># echo $JAVA_HOME<br>usr/java/jdk1.7.0_21<br># <br><br></div>Steve<br><div><br><div><div><div><div> <br></div></div></div></div></div></div><div><br><br><div>
On Mon, Jun 24, 2013 at 1:54 PM, Mark G. Harvey <span dir="ltr">&lt;<a rel="nofollow" href="mailto:markgharvey@yahoo.com" target="_blank">markgharvey@yahoo.com</a>&gt;</span> wrote:<br><blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div><div style="font-size:10pt;font-family:&#39;Courier New&#39;,courier,monaco,monospace,sans-serif"><div>CLUE experts, </div><div><br>This puzzle is likely simple for you folks, but it has me stumped.  I&#39;ve done considerable digging but have found mixed advice. <br>





<br>I&#39;ve created a script to download from a local repo via wget an rpm to install JDK ... no problem there. <br><br>Here&#39;s the part I can&#39;t get right ... setting the variables so I can run scripts to install Tomcat ... Any attempt will bomb if it can&#39;t find /usr/java/jdk1.7.0_21/bin  <br>





<br><br># set JAVA_HOME variable   ... tried in vain<br>echo &quot;setting JAVA_HOME variable variable for the session&quot; <br>JAVA_HOME=/usr/java/jdk1.7.0_21<br># export JAVA_HOME variable  ... tried in vain<br>echo &quot;exporting JAVA_HOME variable variable for the session&quot; <br>





export JAVA_HOME=/usr/java/jdk1.7.0_21<br>echo
 &quot;JAVA_HOME variable: $JAVA_HOME&quot;<br><br># set PATH variable for the session   ... tried in vain<br>echo &quot;setting PATH variable for the session&quot; <br>PATH=$PATH:/usr/java/jdk1.7.0_21/bin<br># export PATH variable for the session<br>





echo &quot;exporting PATH variable for the subsequent sessions &amp; processes&quot; <br>export PATH=$PATH:/usr/java/jdk1.7.0_21/bin<br>echo &quot;show PATH variable: $PATH&quot;<br><br>sleep 3  # wait<br><br># create script to set JAVA_HOME &amp; PATH variables in /etc/profile.d/00_jdk.sh script for all accounts<br>





# use echo command with single quotes to write the literal statement to the script<br>touch /etc/profile.d/00_jdk.sh<br>echo &#39;#!/bin/bash&#39; &gt;&gt; /etc/profile.d/00_jdk.sh<br>echo &#39;# set JAVA_HOME in /etc/profile.d/00_jdk.sh script for all accounts&#39; &gt;&gt; /etc/profile.d/00_jdk.sh<br>





echo &#39;JAVA_HOME=/usr/java/jdk1.7.0_21&#39; &gt;&gt; /etc/profile.d/00_jdk.sh<br>echo &#39;# set PATH in /etc/profile.d/00_jdk.sh
 script for all accounts&#39; &gt;&gt; /etc/profile.d/00_jdk.sh<br>echo &#39;PATH=$PATH:/usr/java/jdk1.7.0_21/bin&#39; &gt;&gt; /etc/profile.d/00_jdk.sh  <br><br>sleep 3  # wait<br><br>echo &quot;review contents of /etc/profile.d/00_jdk.sh&quot;<br>





cat /etc/profile.d/00_jdk.sh<br><br><br>... results of install ... added some blank lines for readability ...<br><br>[root@87148-mondev01 ~]# ./<a rel="nofollow" href="http://dev_install_jdk1.7-1.0.0.sh/" target="_blank">DEV_install_jdk1.7-1.0.0.sh</a> <br>





installation:  Oracle/Sun jdk 1.7 64-bit<br>remount /tmp with execute privledge<br>changed to /tmp<br>Pulling package from Artifactory Repo Management Server<br>--2013-06-24 13:24:38--  <a rel="nofollow">https://&lt;RepoHost&gt;/artifactory/simple/ext-release-local/oracle/jdk/7u21-linux/jdk-7u21-linux-x64.rpm</a><br>





Resolving &lt;RepoHost&gt;... 10.33.44.10<br>Connecting to &lt;RepoHost&gt;|10.33.44.10|:443...
 connected.<br><br>HTTP request sent, awaiting response... 200 OK<br><br>Length: 85388149 (81M) [application/x-rpm]<br>Saving to: “jdk-7u21-linux-x64.rpm”<br><br>100%[=======================================================================================================================================&gt;] 85,388,149  52.7M/s   in 1.5s    <br>





<br>2013-06-24 13:24:40 (52.7 MB/s) - “jdk-7u21-linux-x64.rpm” saved [85388149/85388149]<br><br>jdk downloaded<br>check /tmp/ contents for jdk<br>-rw-r--r--. 1 root root 85388149 Jun  6 16:46 jdk-7u21-linux-x64.rpm<br>install jdk rpm<br>





Preparing...                ########################################### [100%]<br>package jdk-2000:1.7.0_21-fcs.x86_64 is already installed      ........ due to subsequent running of this script<br>install jdk complete<br>





<br>setting JAVA_HOME variable variable for the session<br>exporting
 JAVA_HOME variable variable for the session<br><br>... when tested in the script, the correct answer shows up ... <br><br>JAVA_HOME variable: /usr/java/jdk1.7.0_21   <br><br>setting PATH variable for the session<br>exporting PATH variable for the subsequent sessions &amp; processes<br>





<br>... when tested in the script, the correct answer shows up ... <br><br><br>show PATH variable: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/java/jdk1.7.0_21/bin:/usr/java/jdk1.7.0_21/bin<br>





<br>review contents of /etc/profile.d/00_jdk.sh<br>#!/bin/bash<br># set JAVA_HOME in /etc/profile.d/00_jdk.sh script for all accounts<br>JAVA_HOME=/usr/java/jdk1.7.0_21<br># set PATH in /etc/profile.d/00_jdk.sh  script for all accounts<br>





PATH=$PATH:/usr/java/jdk1.7.0_21/bin<br>remount /tmp removing execute privledge<br>finished<br><br><br>... after script runs, when tested from CLI, variables not not correct
 ... <br><br>[root@87148-mondev01 ~]# echo $JAVA_HOME<br><br>[root@87148-mondev01 ~]# echo $PATH<br>/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin<br><br><br><br>[root@87148-mondev01 ~]# exit<br>logout<br>





<br>[vwadmin@87148-mondev01 ~]$ su -<br>Password: <br><br><br>... now the variable additions show up ... they come from the /etc/profile.d/00_jdk.sh script created as part of the JDK download &amp; install ... <br><br>[root@87148-mondev01 ~]# echo $PATH<br>





/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.7.0_21/bin:/root/bin<br><br>[root@87148-mondev01 ~]# echo $JAVA_HOME<br>/usr/java/jdk1.7.0_21<br><br><br><br>... How can I get the variables to be available for the current root session &amp; usable for subsequent installations?  Trying to avoid the logout / login ... <br>





<br><br><br>Thanks for your help.  <br></div></div></div><br>_______________________________________________<br>
clue mailing list: <a rel="nofollow" href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>
For information, account preferences, or to unsubscribe see:<br>
<a rel="nofollow" href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br></blockquote></div><br></div>
<br>_______________________________________________<br>clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>For information, account preferences, or to unsubscribe see:<br><a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a></div>




</div></div><br>_______________________________________________<br>clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>For information, account preferences, or to unsubscribe see:<br>




<a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br><br></div> </div></div></div> </div> </blockquote></div>   </div></div><br>_______________________________________________<br>





clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>
For information, account preferences, or to unsubscribe see:<br>
<a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
clue mailing list: <a href="mailto:clue@cluedenver.org" target="_blank">clue@cluedenver.org</a><br>
For information, account preferences, or to unsubscribe see:<br>
<a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
clue mailing list: <a href="mailto:clue@cluedenver.org">clue@cluedenver.org</a><br>
For information, account preferences, or to unsubscribe see:<br>
<a href="http://cluedenver.org/mailman/listinfo/clue" target="_blank">http://cluedenver.org/mailman/listinfo/clue</a><br></blockquote></div><br></div>