{"id":36,"date":"2024-02-17T23:14:49","date_gmt":"2024-02-17T23:14:49","guid":{"rendered":"https:\/\/courses.usnh.domains\/cms\/?p=36"},"modified":"2024-02-17T23:18:27","modified_gmt":"2024-02-17T23:18:27","slug":"variables","status":"publish","type":"post","link":"https:\/\/courses.usnh.domains\/cms\/it-612\/variables\/","title":{"rendered":"Variables"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Bash Variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As in any other programming language, you can use variables in Bash Scripting as well. However, there are no data types, and a variable in Bash can contain numbers as well as characters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To assign a value to a variable, all you need to do is use the <code>=<\/code> sign: name=&#8221;DevDojo&#8221;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">{notice} as an important note, you can not have spaces before and after the <code>=<\/code> sign.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">After that, to access the variable, you have to use the <code>$<\/code> and reference it as shown below: echo $name<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Wrapping the variable name between curly brackets is not required, but is considered a good practice, and I would advise you to use them whenever you can: echo ${name}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above code would output: <code>DevDojo<\/code> as this is the value of our <code>name<\/code> variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, let&#8217;s update our <code>devdojo.sh<\/code> script and include a variable in it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, you can open the file <code>devdojo.sh<\/code> with your favorite text editor, I&#8217;m using nano here to open the file: nano devdojo.sh<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Adding our <code>name<\/code> variable here in the file, with a welcome message. Our file now looks like this: #!\/bin\/bash name=&#8221;DevDojo&#8221; echo &#8220;Hi there $name&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save it and run the file using the command below: .\/devdojo.sh<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You would see the following output on your screen: Hi there DevDojo<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a rundown of the script written in the file:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>#!\/bin\/bash<\/code> &#8211; At first, we specified our shebang.<\/li>\n\n\n\n<li><code>name=DevDojo<\/code> &#8211; Then, we defined a variable called <code>name<\/code> and assigned a value to it.<\/li>\n\n\n\n<li><code>echo \"Hi there $name\"<\/code> &#8211; Finally, we output the content of the variable on the screen as a welcome message by using <code>echo<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can also add multiple variables in the file as shown below: #!\/bin\/bash name=&#8221;DevDojo&#8221; greeting=&#8221;Hello&#8221; echo &#8220;$greeting $name&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save the file and run it again: .\/devdojo.sh<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You would see the following output on your screen: Hello DevDojo<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that you don&#8217;t necessarily need to add semicolon <code>;<\/code> at the end of each line. It works both ways, a bit like other programming language such as JavaScript!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also add variables in the Command Line outside the Bash script and they can be read as parameters: .\/devdojo.sh Bobby buddy!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This script takes in two parameters <code>Bobby<\/code>and <code>buddy!<\/code> seperated by space. In the <code>devdojo.sh<\/code> file we have the following: #!\/bin\/bash echo &#8220;Hello there&#8221; $1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>$1<\/code> is the first input (<code>Bobby<\/code>) in the Command Line. Similarly, there could be more inputs and they are all referenced to by the <code>$<\/code> sign and their respective order of input. This means that <code>buddy!<\/code> is referenced to using <code>$2<\/code>. Another useful method for reading variables is the <code>$@<\/code> which reads all inputs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So now let&#8217;s change the <code>devdojo.sh<\/code> file to better understand: #!\/bin\/bash echo &#8220;Hello there&#8221; $1 # $1 : first parameter echo &#8220;Hello there&#8221; $2 # $2 : second parameter echo &#8220;Hello there&#8221; $@ # $@ : all<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ouput for: .\/devdojo.sh Bobby buddy!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Would be the following: Hello there Bobby Hello there buddy! Hello there Bobby buddy!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Derived from: <a href=\"https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/bobbyiliev\/introduction-to-bash-scripting<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash Variables As in any other programming language, you can use variables in Bash Scripting as well. However, there are no data types, and a variable in Bash can contain numbers as well as characters. To assign a value to a variable, all you need to do is use the = sign: name=&#8221;DevDojo&#8221; {notice} as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[8],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-bash","category-it-612","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":1,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/posts\/36\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.usnh.domains\/cms\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}