<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amr Abdeen</title>
    <description>The latest articles on DEV Community by Amr Abdeen (@amrhrabdeen).</description>
    <link>https://dev.to/amrhrabdeen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F421239%2F84bdc06b-d30f-4ab5-8531-63b77ff20473.jpeg</url>
      <title>DEV Community: Amr Abdeen</title>
      <link>https://dev.to/amrhrabdeen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amrhrabdeen"/>
    <language>en</language>
    <item>
      <title>Difference between executing a script and sourcing it in Linux</title>
      <dc:creator>Amr Abdeen</dc:creator>
      <pubDate>Thu, 02 Jul 2020 08:08:14 +0000</pubDate>
      <link>https://dev.to/amrhrabdeen/difference-between-executing-a-script-and-sourcing-it-in-linux-570f</link>
      <guid>https://dev.to/amrhrabdeen/difference-between-executing-a-script-and-sourcing-it-in-linux-570f</guid>
      <description>&lt;p&gt;Sourcing a script will run the commands in the current shell process.&lt;/p&gt;

&lt;p&gt;Executing a script will run the commands in a new shell process.&lt;/p&gt;

&lt;p&gt;Use source if you want the script to change the environment in your currently running shell. use execute otherwise.&lt;/p&gt;

&lt;p&gt;If you are still confused, please read on.&lt;br&gt;
Terminology&lt;/p&gt;

&lt;p&gt;To clarify some common confusion about the syntax to execute and the syntax to source:&lt;/p&gt;

&lt;p&gt;./myscript&lt;/p&gt;

&lt;p&gt;This will execute myscript provided that the file is executable and located in the current directory. The leading dot and slash (./) denotes the current directory. This is necessary because the current directory is usually not (and usually should not be) in $PATH.&lt;/p&gt;

&lt;p&gt;myscript&lt;/p&gt;

&lt;p&gt;This will execute myscript if the file is executable and located in some directory in $PATH.&lt;/p&gt;

&lt;p&gt;source myscript&lt;/p&gt;

&lt;p&gt;This will source myscript. The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.&lt;/p&gt;

&lt;p&gt;. myscript&lt;/p&gt;

&lt;p&gt;This will also source myscript. This "spelling" is the official one as defined by POSIX. Bash defined source as an alias to the dot.&lt;br&gt;
Demonstration&lt;/p&gt;

&lt;p&gt;Consider myscript.sh with following content:&lt;/p&gt;

&lt;h1&gt;
  
  
  !/bin/sh
&lt;/h1&gt;

&lt;h1&gt;
  
  
  demonstrate setting a variable
&lt;/h1&gt;

&lt;p&gt;echo "foo: "$(env | grep FOO)&lt;br&gt;
export FOO=foo&lt;br&gt;
echo "foo: "$(env | grep FOO)&lt;/p&gt;

&lt;h1&gt;
  
  
  demonstrate changing of working directory
&lt;/h1&gt;

&lt;p&gt;echo "PWD: "$PWD&lt;br&gt;
cd somedir&lt;br&gt;
echo "PWD: "$PWD&lt;/p&gt;

&lt;p&gt;Before we execute the script first we check the current environment:&lt;/p&gt;

&lt;p&gt;$ env | grep FOO&lt;br&gt;
$ echo $PWD&lt;br&gt;
/home/lesmana&lt;/p&gt;

&lt;p&gt;The variable FOO is not defined and we are in the home directory.&lt;/p&gt;

&lt;p&gt;Now we execute the file:&lt;/p&gt;

&lt;p&gt;$ ./myscript.sh&lt;br&gt;
foo:&lt;br&gt;
foo: FOO=foo&lt;br&gt;
PWD: /home/lesmana&lt;br&gt;
PWD: /home/lesmana/somedir&lt;/p&gt;

&lt;p&gt;Check the environment again:&lt;/p&gt;

&lt;p&gt;$ env | grep FOO&lt;br&gt;
$ echo $PWD&lt;br&gt;
/home/lesmana&lt;/p&gt;

&lt;p&gt;The variable FOO is not set and the working directory did not change.&lt;/p&gt;

&lt;p&gt;The script output clearly shows that the variable was set and the directory was changed. The check afterwards show that the variable is not set and the directory not changed. What happened? The changes were made in a new shell. The current shell spawned a new shell to run the script. The script is running in the new shell and all changes to the environment take effect in the new shell. After the script is done the new shell is destroyed. All changes to the environment in the new shell are destroyed with the new shell. Only the output text is printed in the current shell.&lt;/p&gt;

&lt;p&gt;Now we source the file:&lt;/p&gt;

&lt;p&gt;$ source myscript.sh&lt;br&gt;
foo:&lt;br&gt;
foo: FOO=foo&lt;br&gt;
PWD: /home/lesmana&lt;br&gt;
PWD: /home/lesmana/somedir&lt;/p&gt;

&lt;p&gt;Check the environment again:&lt;/p&gt;

&lt;p&gt;$ env | grep FOO&lt;br&gt;
FOO=foo&lt;br&gt;
$ echo $PWD&lt;br&gt;
/home/lesmana/somedir&lt;/p&gt;

&lt;p&gt;The variable FOO is set and the working directory has changed.&lt;/p&gt;

&lt;p&gt;Sourcing the script does not create a new shell. All commands are run in the current shell and changes to the environment take effect in the current shell.&lt;/p&gt;

&lt;p&gt;Note that in this simple example the output of executing is the same as sourcing the script. This is not necessarily always the case.&lt;br&gt;
Another Demonstration&lt;/p&gt;

&lt;p&gt;Consider following script pid.sh:&lt;/p&gt;

&lt;h1&gt;
  
  
  !/bin/sh
&lt;/h1&gt;

&lt;p&gt;echo $$&lt;/p&gt;

&lt;p&gt;(the special variable $$ expands to the PID of the current running shell process)&lt;/p&gt;

&lt;p&gt;First print the PID of the current shell:&lt;/p&gt;

&lt;p&gt;$ echo $$&lt;br&gt;
25009&lt;/p&gt;

&lt;p&gt;Source the script:&lt;/p&gt;

&lt;p&gt;$ source pid.sh&lt;br&gt;
25009&lt;/p&gt;

&lt;p&gt;Execute the script, note the PID:&lt;/p&gt;

&lt;p&gt;$ ./pid.sh&lt;br&gt;
25011&lt;/p&gt;

&lt;p&gt;Source again:&lt;/p&gt;

&lt;p&gt;$ source pid.sh&lt;br&gt;
25009&lt;/p&gt;

&lt;p&gt;Execute again:&lt;/p&gt;

&lt;p&gt;$ ./pid.sh&lt;br&gt;
25013&lt;/p&gt;

&lt;p&gt;You can see that sourcing the script runs in the same process while executing the script creates a new process everytime. That new process is the new shell which was created for the execution of the script. Sourcing the script does not create a new shell and thus the PID stays the same.&lt;br&gt;
Summary&lt;/p&gt;

&lt;p&gt;Both sourcing and executing the script will run the commands in the script line by line, as if you typed those commands by hand line by line.&lt;/p&gt;

&lt;p&gt;The differences are:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.
When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Use source if you want the script to change the environment in your currently running shell. use execute otherwise.&lt;/p&gt;

&lt;p&gt;See also:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash
https://askubuntu.com/questions/182012/is-there-a-difference-between-and-source-in-bash-after-all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;==============================================&lt;br&gt;
src:&lt;br&gt;
&lt;a href="https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it"&gt;https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Small Footprint</title>
      <dc:creator>Amr Abdeen</dc:creator>
      <pubDate>Wed, 01 Jul 2020 05:53:36 +0000</pubDate>
      <link>https://dev.to/amrhrabdeen/small-foorprint-jb7</link>
      <guid>https://dev.to/amrhrabdeen/small-foorprint-jb7</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pTF_nE4a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/1618065/what-is-meaning-of-small-footprint-in-terms-of-programming/1618070#1618070" rel="noopener noreferrer"&gt;
              &lt;span class="title-flare"&gt;answer&lt;/span&gt; re:  What is meaning of small footprint in terms of programming?
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Oct 24 '09&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/1618065/what-is-meaning-of-small-footprint-in-terms-of-programming/1618070#1618070" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5MiFESHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          44
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rk_a5QFN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;Footprint designates the size occupied by your application in computer RAM memory.&lt;/p&gt;
&lt;p&gt;Footprint can have different meaning when speaking about memory consumption
In my experience, memory footprint often doesn't include memory allocated on the heap (dynamic memory), or resource loaded from disc etc. This is because dynamic allocations are non…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/1618065/what-is-meaning-of-small-footprint-in-terms-of-programming/1618070#1618070" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
  </channel>
</rss>
