{"id":7901,"date":"2019-01-12T07:12:00","date_gmt":"2019-01-12T07:12:00","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=7901"},"modified":"2019-01-12T08:37:27","modified_gmt":"2019-01-12T08:37:27","slug":"bash-declare-command-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/12\/bash-declare-command-linux-hint\/","title":{"rendered":"Bash declare command \u2013 Linux Hint"},"content":{"rendered":"<p>Bash doesn\u2019t have a strong type system. To allow type-like behavior, it uses attributes that can be set by a command.<\/p>\n<p>\u2018declare\u2019<\/p>\n<p>is a bash built-in command that allows you to update attributes applied to variables within the scope of your shell. In addition, it can be used to declare a variable in longhand. Lastly, it allows you to peek into variables.<\/p>\n<p>Here you will find out that you are blind or using the bash declare command.<\/p>\n<p>At this point you are thinking, what do I need to know to use the declare command in bash? At the time like these, the man command comes in handy. I\u2019m just going to paste the part about declare in bash builtins here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/1-20.png\" alt=\"\" width=\"642\" height=\"563\" \/><\/p>\n<p>Here are some help commands to see what it looks like in your terminal. Note that the last one is a failsafe for our friends running Git Bash in Windows.<\/p>\n<p>Help commands for bash declare<\/p>\n<ul>\n<li>man bash (and find the section about declare<\/li>\n<li>or bash help declare<\/li>\n<\/ul>\n<p>Now that you have read the primer, man page for declare in bash, it is time to get our hands dirty with some examples of bash declare in the wild. Note that as you scroll down deep into the jungle of bash declare examples, your pay grade and level of understanding of declare will improve.<\/p>\n<p>First let\u2019s start out by seeing if anyone declared a variable called bar. If no one has yet, dibs!<\/p>\n<p>If you see the error bash: declare: bar: not found, then no one has yet. Let\u2019s just echo $? to be sure.<\/p>\n<p>1, okay good. Otherwise, you should see something like declare &#8212; bar=&#8221;&#8221;. If you haven\u2019t yet, go ahead and declare bar as something, bar= or declare bar= should do the trick. Note that the latter of the two is the longhand for variables in bash. If you are wondering what the &#8212; in declare output is, that is where variable attributes go, and there are none.<\/p>\n<p>Now that assigning variables using declare is out of the picture, let\u2019s start giving them attributes.<\/p>\n<p>If you are running bash v4.3-alpha or later, this section on the -n option. If you are not sure, check using the bash &#8211;version command. Otherwise, don\u2019t try this at home.<\/p>\n<p>Look at that. We just asigned a variable to another by name. Look what happens here.<\/p>\n<p>bar=x<br \/>\ndeclare -n foo=bar<br \/>\necho $ $ # x x<br \/>\nfoo=y<br \/>\necho $ $ # y y<br \/>\ntrue<\/p>\n<p>Now look what happens when when we don\u2019t use declare with the -n option.<\/p>\n<p>bar=x<br \/>\ndeclare foo=bar<br \/>\necho $ $ # x x<br \/>\nfoo=y<br \/>\necho $ $ # y x<br \/>\ntrue<\/p>\n<h2><b>Exports<\/b><\/h2>\n<p>Now suppose that we tried to do something odd like this:<\/p>\n<p>echo{,} $ &gt; echo-bar.sh<br \/>\nbash echo-bar.sh<\/p>\n<p>As you may suspect, nothing happened in standard output. Don\u2019t worry about the voodoo in the first line. Programmers are lazy. The declare command can make names export!<\/p>\n<p>declare -x bar # export bar<\/p>\n<p>Now give it a try.<\/p>\n<p>Note that using the \u2013x option for declare can also be done through the export command as follows. Be sure to open up a new shell or remove the attribute using the +x option before trying out the following example.<\/p>\n<p>bar=x<br \/>\necho{,} $ &gt; echo-bar.sh<br \/>\nbash echo-bar.sh #<br \/>\nexport bar<br \/>\nbash echo-bar.sh # x<\/p>\n<h3><b>Integers<\/b><\/h3>\n<p>In bash, variables may have the integer attribute and the only way to accomplish this is through declare command.<\/p>\n<p>Suppose that we are dealing with integers and want to make our variables behavior more responsible. We could give such variables the integer attribute using the \u2013i option for declare.<\/p>\n<p>declare \u2013i bar # don\u2019t know what\u2019s in bar anymore but now it\u2019s an integer<br \/>\necho $ # x (maybe)<br \/>\nbar=x<br \/>\necho $ # 0<br \/>\nbar=1<br \/>\necho $ # 1<br \/>\nbar=3.14 # ouch<br \/>\ntrue<\/p>\n<p>Note that now when we try to assign a new value to our variable 3 things happen: 1) The value is interpreted as 0; 2) The value is interpreted as an integer; 3) Error.<\/p>\n<p>In addition to modifying the value assignment behavior, variables now behavior differently in arithmetic expressions as follows.<\/p>\n<p>declare -i bar=1<br \/>\ndeclare car=1<br \/>\necho $ # 1<br \/>\necho $ # 1<br \/>\nbar=bar+1<br \/>\ncar=car+1<br \/>\necho $ # 2<br \/>\necho $ # car+1<br \/>\ntrue<\/p>\n<p>Note that you can still get away with using a variable to store an integer and carry out arithmetic without setting the integer attribute for a variable but it is there just in case.<\/p>\n<h3><b>Cases<\/b><\/h3>\n<p>In bash, variables may have case attributes applied on assignment. Declare allows conversion to cases lower or upper if \u2013l or \u2013u options are set, respectfully.<\/p>\n<p>declare -u uppers=<br \/>\ndeclare -l lowers=<br \/>\nuppers=uppercase<br \/>\nlowers=LOWERCASE<br \/>\necho $ # UPPERCASE<br \/>\necho $ # lowercase<br \/>\necho $ # uppercase<br \/>\necho $ # LOWERCASE<br \/>\ntrue<\/p>\n<p>These attributes may come in handy if you require single case without having to do the conversion yourself.<\/p>\n<h3><b>Readonly<\/b><\/h3>\n<p>In bash, variable may be readonly. To accomplish this there is the -r option for declare.<\/p>\n<p>declare \u2013r lowers # try to make lowers final<br \/>\nlowers=&#8221;Yet another lowers&#8221;<br \/>\necho $ # yet another lowers<br \/>\ndeclare -rl final_lowers=&#8221;Yet another lowers&#8221;<br \/>\necho $ # yet another lowers<br \/>\nfinal_lowers=&#8221;Yet again another lowers&#8221; # assignment block<br \/>\ntrue<\/p>\n<p>This attribute could come in handy if you know that a variable has no business being changed after assignment. Note that the +r option does not work; that is stripping a variable of its readonly attribute is not allowed in bash.<\/p>\n<h3><b>Arrays<\/b><\/h3>\n<p>In bash, variables may be arrays. To make a variable an associative or indexed array, the \u2013A and \u2013a options for declare are used, respectfully.<\/p>\n<p>declare -a indexed_array<br \/>\ndeclare -A associative_array<br \/>\nindexed_array[0]=1<br \/>\nassociative_array[0]=1<br \/>\nindexed_array[one]=2 # ?<br \/>\nassociative_array[one]=2<br \/>\necho $ # 2<br \/>\necho $ # 1<br \/>\necho $ # 2<br \/>\necho $ # 2<br \/>\ndeclare -p indexed_array<br \/>\ndeclare -p associative_array<br \/>\necho $ # ouch<br \/>\ntrue<\/p>\n<p>In most programming languages having the ability to use arrays is a powerful construct. Bash is no exception. It allows this through array attributes which could come in handy if requiring hash lookup or in implementing object-like behavior. Note that the index of indexed arrays behaviors like a variable with the integer attribute, thus is expected to break in the same manner, hence the last line before true.<\/p>\n<h3><b>Trace<\/b><\/h3>\n<p>In bash, variable may have the trace attribute applied via the -t option in declare. Trace variables unlike variables with other attributes applied depend heavily on the environment of the calling shell.<\/p>\n<p>I have found mixed results using the trace attribute which have led to a review on traps and applications of trapping the DEBUG and RETURN signal. For those that tinker, finding a use for declaring a variable with the -t option is extra credit.<\/p>\n<h3><b>Functions<\/b><\/h3>\n<p>In bash, one of the most useful uses for the declare command is being able to display functions. The -f and -F options for declare display definition and just function names if available, respectfully.<\/p>\n<p>Suppose that you want to have a fallback in case a function is not defined in your shell. We can use declare to accomplish this task as follows. For simplicity sakes, let\u2019s use a function called foo.<\/p>\n<p># if foo is not declared<\/p>\n<p># declare it# else use available footest ! &#8220;$( declare -F foo )\u201d || {<br \/>\nfoo() { true ; }<br \/>\n}<\/p>\n<p>For those that tinker, there is an alias using called <a href=\"https:\/\/github.com\/temptemp3\/sh2\/blob\/odys\/aliases\/commands.sh\">commands<\/a> that I cooked up a while back that uses declare to check if functions are available.<\/p>\n<h4>Conclusion<\/h4>\n<p>Although most programmers can get away with not having to use it at all, like most builtins, the declare command in bash is an essential command to really know your way around the bash shell.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/bash_declare_command\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash doesn\u2019t have a strong type system. To allow type-like behavior, it uses attributes that can be set by a command. \u2018declare\u2019 is a bash built-in command that allows you to update attributes applied to variables within the scope of your shell. In addition, it can be used to declare a variable in longhand. Lastly, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/12\/bash-declare-command-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash declare command \u2013 Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7901","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=7901"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7901\/revisions"}],"predecessor-version":[{"id":7915,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/7901\/revisions\/7915"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=7901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=7901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=7901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}