2018-11-13 14:32:22 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
category-page: scripts
|
|
|
|
category-title: Scripting
|
2018-11-14 13:18:35 +00:00
|
|
|
tags: parameter expansion brace variable check condition empty exists
|
2018-11-13 14:32:22 +00:00
|
|
|
author: Marco Tereh
|
|
|
|
title: Parameter expansion
|
2018-11-18 20:38:56 +00:00
|
|
|
previous-page: pages/scripts/2-special-variables.html
|
|
|
|
next-page: pages/scripts/4-arrays.html
|
2018-11-13 14:32:22 +00:00
|
|
|
---
|
|
|
|
|
2018-11-15 20:09:21 +00:00
|
|
|
There are some special operations that can be performed on
|
|
|
|
<a href="variables.html">variables</a> and strings called parameter expansions.
|
2018-11-13 14:32:22 +00:00
|
|
|
The general syntax for all parameter expansions is this one:
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${CODE_HERE}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
|
|
|
Depending on what you want to do with your variable, the code
|
|
|
|
that goes inside the braces differs.<br>
|
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${VARIABLE:-DEFAULT_VALUE}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
|
|
|
If the variable VARIABLE exists and has a value (i.e. it is not null), this is equal to
|
|
|
|
the value of VARIABLE.
|
2018-11-13 14:32:22 +00:00
|
|
|
Otherwise, it is equal to DEFAULT_VALUE.<br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:32:22 +00:00
|
|
|
Example: <code>echo "First name: ${firstname:-John}";</code><br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${VARIABLE:=DEFAULT_VALUE}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:32:22 +00:00
|
|
|
If the variable VARIABLE exists and has a value, this is equal to the value of VARIABLE.
|
|
|
|
Otherwise, it sets the variable to DEFAULT_VALUE and is equal to it.<br>
|
|
|
|
Example: <code>echo "Last name: ${lastname:=Doe}";</code><br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${VARIABLE:?ERR_VALUE}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:32:22 +00:00
|
|
|
If the variable VARIABLE exists and has a value, this is equal to the value of VARIABLE.
|
2018-11-15 20:09:21 +00:00
|
|
|
Otherwise, it prints ERR_VALUE to STDERR and exits (meaning nothing else will be executed
|
|
|
|
after this).<br>
|
2018-11-13 14:32:22 +00:00
|
|
|
Example: <code>currdate=${date:?Operation failed: date unknown};</code><br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${VARIABLE:+VALUE}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:32:22 +00:00
|
|
|
If the variable VARIABLE exists and has a value, this is equal to VALUE.
|
|
|
|
Otherwise, this has no effect.<br>
|
2018-11-15 20:09:21 +00:00
|
|
|
Example: <code>echo -e "reading from address $read.${write:+\nWARNING: read and write set
|
|
|
|
at the same time}";</code><br>
|
|
|
|
|
2018-11-13 14:32:22 +00:00
|
|
|
All of these can also be written without the colon, in which case their meaning changes to
|
|
|
|
"If the variable VARIABLE exists at all (even if it is null), this is ..."<br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${VARIABLE: NUMBER}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
|
|
|
This is equal to the substring of the value of VARIABLE, starting at the character with
|
|
|
|
(0-based) index NUMBER.<br>
|
2018-11-13 14:32:22 +00:00
|
|
|
If NUMBER is negative, the substring starts NUMBER characters before the end of the string.<br>
|
|
|
|
Example: <code>lastname=${fullname:$firstnamelength};</code><br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${VARIABLE: FROM:LENGTH}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
|
|
|
This is equal to the substring of the value of VARIABLE, starting at the character with (0-based)
|
|
|
|
index FROM with length LENGTH<br>
|
2018-11-13 14:32:22 +00:00
|
|
|
If FROM is negative, the substring starts FROM characters before the end of the string.<br>
|
|
|
|
Example: <code>lastname=${middlename:$firstnamelength:$middlenamelength};</code><br>
|
2018-11-14 06:22:53 +00:00
|
|
|
<!-- If/when somebody makes the page on arithmetic operations, mention that NUMBER can be an arithmetic operation as well
|
|
|
|
and link it. -->
|
2018-11-13 14:56:12 +00:00
|
|
|
{% highlight bash %}
|
2018-11-13 14:32:22 +00:00
|
|
|
${#VARIABLE}
|
2018-11-13 14:56:12 +00:00
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-13 14:32:22 +00:00
|
|
|
This is equal to the length of the value of VARIABLE<br>
|
|
|
|
Example: <code>echo "your name has ${#name} characters";</code><br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 06:22:53 +00:00
|
|
|
Paremeter expansions can also be nested, like this:
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 06:22:53 +00:00
|
|
|
{% highlight bash %}
|
|
|
|
${input:?${INVALID_INPUT_ERR_MSG:-An unknown error occurred}}
|
|
|
|
{% endhighlight %}
|
2018-11-15 20:09:21 +00:00
|
|
|
|
|
|
|
Further reading: <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion">
|
|
|
|
the bash reference manual</a>
|