Bash set: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 40: Line 40:
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:'''Note''' you must set IFS back to " " before using it split the arguments as shown above. This is done with IFS=" ".<br>
:'''Note''' you must set IFS back to " " before using it split the arguments as shown above. This is done with IFS=" ".<br>
</blockquote>
For more details about IFS see:
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[bash Environment Variables#IFS|IFS]]
</blockquote>
</blockquote>

Revision as of 23:34, 29 February 2016

External

Internal

set and Positional Parameters

arg="a b c"
set ${arg}

assigns "a", "b", "c" to positional parameters $1, $2 and $3.

Note that if any of the space-separated argument contains '--', set will try to interpret that sequence of characters:

set: --: invalid option

so in order to turn it back into positional argument, you will have to ??? do something, did not figure it out yet.

List Separator

The default list separator is space. That can be changed by setting the value of the IFS variable before executing set.

Example:

arg="a,b,c"
IFS=","
set ${arg}

will identically assign "a", "b", "c" to positional parameters $1, $2 and $3.

Note you must set IFS back to " " before using it split the arguments as shown above. This is done with IFS=" ".

For more details about IFS see:

IFS