{"id":8933,"date":"2021-08-03T05:58:38","date_gmt":"2021-08-03T05:58:38","guid":{"rendered":"https:\/\/linuxways.net\/?p=8933"},"modified":"2021-08-03T05:58:46","modified_gmt":"2021-08-03T05:58:46","slug":"source-command-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/source-command-in-linux\/","title":{"rendered":"Source Command in Linux"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Source is a command to read a file then execute its contents. It helps to load variables, functions, and configuration files into shell scripts.<\/p>\n<p>Source is a shell built-in command and some shells in Linux and UNIX. It passed as an argument in the current shell script.<\/p>\n<p>This article will show you how to use the source command in Linux as we go through below.<\/p>\n<h2>The syntax of source command<\/h2>\n<pre>$ source FILENAME [ARGUMENTS]<\/pre>\n<p>We can replace source command by the .(dot)<\/p>\n<pre>$ . FILENAME [ARGUMENTS]<\/pre>\n<h2>How to use source command<\/h2>\n<p><strong>1. Refresh current shell environment<\/strong><\/p>\n<p>When using Linux, a user can define alias in the current shell environment. For example, to display hidden files, we use ls -la. But we can use faster way by alias:<\/p>\n<pre>$ alias la=\u2018ls -la\u2019<\/pre>\n<p>Then we only type la to display hidden file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"626\" class=\"wp-image-8934\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-24.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-24.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-24-300x200.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-24-768x511.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>But this is only temporary, for permanent use, open the<strong> .bashrc<\/strong> file and type:<\/p>\n<pre>alias la= \u2018ls -la\u2019<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"492\" class=\"wp-image-8935\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-2.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-2.jpeg 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-2-300x157.jpeg 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-2-768x402.jpeg 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>Refresh the current shell environment, type:<\/p>\n<pre>$ source ~\/.bashrc<\/pre>\n<p><strong>2. Execute shell script in the current shell environment<\/strong><\/p>\n<p>A shell script can\u2019t understand the variables you define in the current shell environment. So we must use the source command.<\/p>\n<p>For example, we will try to run apt command<\/p>\n<p>Firstly, let\u2019s create file <strong>update.sh<\/strong> and start with:<\/p>\n<pre>#!bin\/bash<\/pre>\n<p>Secondly, type the following command:<\/p>\n<pre>sudo apt update<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"490\" class=\"wp-image-8936\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-25.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-25.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-25-300x156.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-25-768x400.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>Lastly, save and execute it by source command:<\/p>\n<pre>$ source .\/update.sh<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"272\" class=\"wp-image-8937\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-26.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-26.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-26-300x87.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-26-768x222.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p><strong>3. Import a shell function<\/strong><\/p>\n<p>For example:<\/p>\n<p>Firstly, let\u2019s create file<strong> foo.sh<\/strong> and start with:<\/p>\n<pre>#!bin\/bash<\/pre>\n<p>Secondly, define a custom shell script. Here I put a function named <strong>foo<\/strong>:<\/p>\n<pre>foo () {\r\n\r\necho \u201cHello\u201d\r\n\r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"540\" class=\"wp-image-8938\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-27.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-27.png 939w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-27-300x173.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-27-768x442.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><\/p>\n<p>Let\u2019s save it.<\/p>\n<p>To import the <strong>foo<\/strong> function, run the following command:<\/p>\n<pre>$ source foo.sh<\/pre>\n<p>To run the shell function, type:<\/p>\n<pre>$ foo<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"86\" class=\"wp-image-8939\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-28.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-28.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-28-300x27.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-28-768x70.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p><strong>4. Read and execute commands<\/strong><\/p>\n<p>Let&#8217;s say you want to execute 2 commands ls and df -h, let\u2019s create a file <strong>*txt<\/strong> contains those 2 commands. Here I will create file <strong>example.txt<\/strong>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"540\" class=\"wp-image-8940\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-29.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-29.png 941w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-29-300x172.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-29-768x441.png 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><\/p>\n<p>And save it. Then run source filename:<\/p>\n<pre>$ source example.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"283\" class=\"wp-image-8941\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-3.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-3.jpeg 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-3-300x90.jpeg 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-3-768x231.jpeg 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>You\u2019ve already gone through the details of how to use the source command in Linux.<\/p>\n<p>Thanks for reading.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction Source is a command to read a file then execute its contents. It helps to load variables, functions, and configuration files into shell scripts. Source is a&hellip;<\/p>","protected":false},"author":1,"featured_media":8960,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[461],"class_list":["post-8933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-source-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=8933"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8933\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/8960"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=8933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=8933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=8933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}