{"id":190,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:01","modified_gmt":"2020-08-22T20:26:01","slug":"this-is-the-page-title-toplevel-24","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=190","title":{"rendered":"Functions"},"content":{"rendered":"\n<title>Functions<\/title>\n<p>\nMost (all?) shells have the means of creating new &#8220;internal&#8221; commands. This\nis done by creating <glossary>shell<\/glossary>\n<i>functions<\/i>. Shell functions are just like those\nin a programming language. Sets of commands are grouped together and jointly\ncalled by a single name.\n<\/p>\n<p>\nThe format for functions is:\n<\/p>\n<p>\n<commandexample command=\"bash\">\nfunction_name()\n{\nfirst thing to do\nsecond thing to do\nthird thing to do\n}\n<\/commandexample>\n<p>\nFunctions can be defined anywhere, including from the <glossary>command line<\/glossary>.\nAll you need to do is simply type in the lines one at a time, similar to the way shown\nabove. The thing to bear in mind is that if you type a function from a command\nline, once you exit that <glossary>shell<\/glossary>, the function is gone.\n<\/p>\n<p>\nMost of the time functions are defined within a shell script. As you might expect,\nthe function is only valid within the scope of the scipt. However, if you\n<command name=\"bash\">source<\/source> the shell scripts as we discussed in\nthe <site id=\"22\">section on shell basics<\/site>,\nthen the function is valid for the current shell.\n<\/p>\n<p>\nYou can also pass arguments to shell functions, just like commands. A\nsimple example is a script that looks like this:<\/p>\n<p>\n<commandexample command=\"bash\">\ndisplay()\n{\necho $1\n}\n<\/commandexample>\n<p>\n<commandexample command=\"bash\">display Hello<\/commandexample>\n<p>\n<screenoutput>The output would be<\/screenoutput>\n<\/p>\n<p>\nHello\n<\/p>\n<p>\nHere we need to be careful. The <glossary>variable<\/glossary>\n$1 is the positional parameter from the call to the display function and not to\nany script containing this function. We can see how this works if we\ncreate a the script to look like this:\n<\/p>\n<p>\n<commandexample command=\"bash\">\ndisplay()\n{\necho $1\n}\ndisplay $1\necho $1\n<\/commandexample>\n<\/p>\n<p>\nThis is slightly different than the function by itself in that we first call the function\nitself and then there is an extra\n<command>echo<\/command> at the end. Lets call the script display.sh, change the\npermissions so that it is executable and start it like this:\n<\/p>\n<p>\n<commandexample command=\"bash\">display.sh Hi<\/commandexample>\n<\/p>\n<p>\nThe output would then look like this:\n<\/p>\n<p>\n<screenoutput>Hi<\/screenoutput>\n<\/p>\n<p>\n<screenoutput>Hello<\/screenoutput>\n<p>\nThe first <command>echo<\/command> shows us the parameter from the function and the second\none shows us the parameter from the <glossary>command line<\/glossary>.\n<\/p>\n<p>\nAs you can see, although we are using the same positional parameter ($1), what is assigned to\nit depends on the scope. The parameter <glossary>command line<\/glossary> passed on the command\nline (&#8220;Hi&#8221;) is valid for the entires scope of the script. <i>However<\/i>, it is overwritten within\nthe scope of the function by the new value.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functions Most (all?) shells have the means of creating new &#8220;internal&#8221; commands. This is done by creating shell functions. Shell functions are just like those in a programming language. Sets of commands are grouped together and jointly called by a &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=190\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-190","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=190"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/190\/revisions"}],"predecessor-version":[{"id":565,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/190\/revisions\/565"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}