{"id":183,"date":"2012-05-03T15:20:10","date_gmt":"2012-05-03T22:20:10","guid":{"rendered":"http:\/\/sobell.com\/mgsblog\/?p=183"},"modified":"2012-05-03T15:20:10","modified_gmt":"2012-05-03T22:20:10","slug":"use-to-assign-a-default-value-to-an-unset-variable","status":"publish","type":"post","link":"https:\/\/sobell.com\/mgsblog\/archives\/183","title":{"rendered":"Use := to Assign a Default Value to an Unset Variable"},"content":{"rendered":"<p>You can change the value of a null or unset variable to its default in a script using the <strong>:=<\/strong> modifier:<\/p>\n<pre><code>${name:=default}<\/code><\/pre>\n<p>When the shell expands the expression <strong>${name:=default}<\/strong>, it sets the value of <strong>name<\/strong> to the expanded value of <strong>default<\/strong>. If a script contains a line such as the following and <strong>LIT<\/strong> is unset or null at the time this line is executed, the shell assigns <strong>LIT<\/strong> the value <strong>\/home\/max\/literature<\/strong>:<\/p>\n<pre><code>$ ls ${LIT:=\/home\/max\/literature}<\/code><\/pre>\n<p>Shell scripts frequently start with the <strong>:<\/strong> (null) builtin followed on the same line by the <strong>:=<\/strong> expansion modifier to set any variables that might be null or unset. The <strong>:<\/strong> builtin evaluates each token in the remainder of the command line but does not execute any commands.<br \/>\nUse the following syntax to set a default for a null or unset variable in a shell script (a SPACE follows the first colon). Without the leading colon (<strong>:<\/strong>), the shell would evaluate and attempt to execute the \u201ccommand\u201d that results from the evaluation.<\/p>\n<pre><code>: ${name:=default}<\/code><\/pre>\n<p>When a script needs a directory for temporary files and uses the value of <strong>TEMPDIR<\/strong> for the name of this directory, the following line assigns to <strong>TEMPDIR<\/strong> the value <strong>\/tmp<\/strong> if <strong>TEMPDIR<\/strong> is null:<\/p>\n<pre><code>: ${TEMPDIR:=\/tmp}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can change the value of a null or unset variable to its default in a script using the := modifier: ${name:=default} When the shell expands the expression ${name:=default}, it sets the value of name to the expanded value of &hellip; <a href=\"https:\/\/sobell.com\/mgsblog\/archives\/183\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,6],"tags":[],"_links":{"self":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts\/183"}],"collection":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":13,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts\/183\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}