Bash set: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 15: | Line 15: | ||
</pre> | </pre> | ||
assigns "a", "b", "c" to [[ | assigns "a", "b", "c" to [[Bash_Built-In_Variables#Positional_Parameters_.241.2C_.242.2C_...|positional parameters]] $1, $2 and $3. '-' that follows the builtin signals the end of options and causes all remaining arguments to be assigned to the positional parameters. This 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: | ||
set: --: invalid option | set: --: invalid option | ||
Line 34: | Line 34: | ||
<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 whitespace after setting it to something else, so the basic shell function work as expected. This is done as shown here: [[ | :'''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: [[Bash_Built-In_Variables#Restoring_the_default_IFS_value|restoring the default IFS value]].<br> | ||
</blockquote> | </blockquote> | ||
Line 40: | Line 40: | ||
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | <blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | ||
:[[ | :[[Bash_Built-In_Variables#IFS|IFS]] | ||
</blockquote> | </blockquote> |
Revision as of 01:34, 24 September 2017
External
- set man page http://linuxcommand.org/lc3_man_pages/seth.html
- https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
Internal
set and Positional Parameters
arg="a b c" set - ${arg}
assigns "a", "b", "c" to positional parameters $1, $2 and $3. '-' that follows the builtin signals the end of options and causes all remaining arguments to be assigned to the positional parameters. This 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: