<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>gHacks Technology News &#124; Latest Tech News, Software And Tutorials &#187; bash scripting</title> <atom:link href="http://www.ghacks.net/tag/bash-scripting/feed/" rel="self" type="application/rss+xml" /><link>http://www.ghacks.net</link> <description>A technology news blog covering software, mobile phones, gadgets, security, the Internet and other relevant areas.</description> <lastBuildDate>Sat, 11 Feb 2012 09:52:46 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/> <item><title>Get to know Linux: Bash scripting basics</title><link>http://www.ghacks.net/2009/05/20/get-to-know-linux-bash-scripting-basics/</link> <comments>http://www.ghacks.net/2009/05/20/get-to-know-linux-bash-scripting-basics/#comments</comments> <pubDate>Wed, 20 May 2009 18:23:46 +0000</pubDate> <dc:creator>Jack Wallen</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[Open Source]]></category> <category><![CDATA[Tutorials Basic]]></category> <category><![CDATA[bash]]></category> <category><![CDATA[bash scripting]]></category> <category><![CDATA[Hello World!]]></category> <guid
isPermaLink="false">http://www.ghacks.net/?p=12956</guid> <description><![CDATA[After using Linux for a while you will eventually find yourself needing to create a bash script. And just what is a bash script? A bash script is a script that is run through the bash shell. Usually a bash script is a user-generated script that serves a specific purpose and combines numerous commands into [...]]]></description> <content:encoded><![CDATA[<p>After using Linux for a while you will eventually find yourself needing to create a bash script. And just what is a bash script? A bash script is a script that is run through the bash shell. Usually a bash script is a user-generated script that serves a specific purpose and combines numerous commands into one convenient script.</p><p>I am a big fan of bash scripting. I use them frequently to create backup scripts, scripts that are used via cron, and much more. But before I could write even a simple bash script, I had to understand the basics. And that&#8217;s what you will learn here &#8211; the very basics of bash scripting. This will be a foundation you can build upon so that your bash scripts can get more and more complex.</p><p><span
id="more-12956"></span><strong>The structure</strong></p><p>A bash script consists of just a few pieces. First and foremost you have to actually create the file. This file will contain all of your scripting and will have to be made executable by the user. Once the file is complete and saved you will make this executable with the command:</p><p><em>chmod u+x FILENAME</em></p><p>Where FILENAME is the actual name of your file.</p><p>Now within the file you will have to at least have two minimal pieces:</p><ul><li>Shell declaration</li><li>Script</li></ul><p>The shell declaration is a statement that declares what shell you are to use. For nearly all of your Linux needs you will use the bash shell. To declare the bash shell being used your declaration will be:</p><p><em>#! /bin/bash</em></p><p>With that declaration all commands will be run through the bash shell.</p><p>The script is the contents of the shell script you will write. The script will most often consist of commands.</p><p><strong>Hello world</strong></p><p>Ah hello world! Who hasn&#8217;t or used this as an example. Let&#8217;s take a look at what an Hello World script wold look like. We&#8217;ll add a few variations to highlight some of the subtle differences.</p><p>The basic Hello World! script would look like:</p><p><code>#! /bin/bash<br
/> echo "Hello World!"</code></p><p>Once you save it (we&#8217;ll call it &#8220;hello&#8221;) and make it executable you can run it by issuing the command:</p><p>~/hello</p><p>and you will see the output:</p><p><em>Hello World!</em></p><p>Now let&#8217;s use variable declaration in this script. Using variables will make your scripting much more versatile.</p><p><code>#! /bin/bash<br
/> STRING1="Hello"<br
/> STRING2="World!"<br
/> echo $STRING1 $STRING2</code></p><p>Now let&#8217;s modify this to use a global variable. One useful global variable is USER. At the bash prompt enter <em>echo $USER </em>and bash will return the username that is currently logged in. So change your hello world script to look like:</p><p><code>#! /bin/bash<br
/> STRING1="Hello"<br
/> echo $STRING1 $USER</code></p><p>When you run this script you will see:</p><p><em>Hello USER</em></p><p>Where USER is the actual username logged in. You can test this by su&#8217;ing to a different user (such as root) and running the script. If you are root (make sure you su to root with the command <em>su &#8211; </em>or you won&#8217;t have root&#8217;s prompt, only root&#8217;s privileges) you will see:</p><p>Hello root</p><p><strong>Final thoughts</strong></p><p>And there you have the very fundamentals of bash scripting. Hopefully you can see how to build on the Hello World! example.</p> ]]></content:encoded> <wfw:commentRss>http://www.ghacks.net/2009/05/20/get-to-know-linux-bash-scripting-basics/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> </channel> </rss>
