Bash set: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
<pre>
<pre>
arg="a b c"
arg="a b c"
set ${arg}
set - ${arg}
</pre>
</pre>


assigns "a", "b", "c" to [[Bash_Environment_Variables#Positional_Parameters_.241.2C_.242.2C_...|positional parameters]] $1, $2 and $3.  
assigns "a", "b", "c" to [[Bash_Environment_Variables#Positional_Parameters_.241.2C_.242.2C_...|positional parameters]] $1, $2 and $3. '-' on the set command line prevents arguments that start with "--..." to interfere with [[bash set#Overview|set]] function. If "-" is not specified, and the first of the space-separated arguments contains '--', set will try to interpret that sequence of characters and will produce:


Note that if any of the space-separated argument contains '--', set will try to interpret that sequence of characters:
set: --: invalid option
 
<pre>
set: --: invalid option
</pre>
 
so in order to turn it back into positional argument, you will have to <font color=red>??? do something, did not figure it out yet.</font>


==List Separator==
==List Separator==

Revision as of 01:11, 24 September 2017

External

Internal

set and Positional Parameters

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

assigns "a", "b", "c" to positional parameters $1, $2 and $3. '-' on the set command line prevents arguments that start with "--..." to interfere with set function. If "-" is not specified, and the first of the space-separated arguments contains '--', set will try to interpret that sequence of characters and will produce:

set: --: invalid option

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 whitespace after setting it to something else, so the basic shell function work as expected. This is done as shown here: restoring the default IFS value.

For more details about IFS see:

IFS