{"id":3369,"date":"2014-07-21T23:43:50","date_gmt":"2014-07-21T15:43:50","guid":{"rendered":"http:\/\/rmohan.com\/?p=3369"},"modified":"2014-07-21T23:43:50","modified_gmt":"2014-07-21T15:43:50","slug":"function-in-shell-script","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3369","title":{"rendered":"Function in shell script"},"content":{"rendered":"<p>Function in shell script<br \/>\nDESCRIPTION:<br \/>\nFunction is the piece of code, which executed when we call.<br \/>\nWhen thinking about the function in any computer language, three points comes in mind<\/p>\n<p>  1. Writing function code and calling that function when we need<br \/>\n  2. Passing arguments to the function<br \/>\n  3. Returning value from function<\/p>\n<p>Next i going to elaborate about the 3 points i mentioned above <\/p>\n<p>FUNCTION CALL<br \/>\nScript shows below describe the simple function call. We should define the function before we using that <\/p>\n<p>script:<br \/>\n  #!\/bin\/bash<br \/>\n  display(){<br \/>\n       echo &#8220;This is inside function definition&#8221;<br \/>\n  }<br \/>\n  display #calling the display function <\/p>\n<p>output:<br \/>\n  This is inside function definition<\/p>\n<p>PASSING ARGUMENT<br \/>\nFollowing scripts pass argument to function and display the argument value.<\/p>\n<p>script:<br \/>\n#!\/bin\/bash<\/p>\n<p>display_arg(){<br \/>\n    echo &#8220;first argument : $1&#8221;<br \/>\n    echo &#8220;second argument : $2&#8221;<br \/>\n}<br \/>\ndisplay_arg &#8220;12&#8221; &#8220;sujin&#8221; #passing argument to function<\/p>\n<p>output:<br \/>\nfirst argument : 12<br \/>\nsecond argument : sujin<\/p>\n<p>RETURNING VALUES<br \/>\nThis script will shows how function return value to calling function<\/p>\n<p>script:<br \/>\n#!\/bin\/bash<\/p>\n<p>ret_val(){<br \/>\n    i=&#8221;mystring&#8221;<br \/>\n    echo $i<br \/>\n}<br \/>\nx=$(ret_val)   #passing argument to function<br \/>\necho &#8220;Returned Value : $x&#8221; <\/p>\n<p>output:<br \/>\nReturned Value : mystring<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Function in shell script DESCRIPTION: Function is the piece of code, which executed when we call. When thinking about the function in any computer language, three points comes in mind<\/p>\n<p> 1. Writing function code and calling that function when we need 2. Passing arguments to the function 3. Returning value from function<\/p>\n<p>Next i [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3369"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3369"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3369\/revisions"}],"predecessor-version":[{"id":3370,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3369\/revisions\/3370"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}