Can IFS be changed locally in a Bash function?

bash ifs
bash ifs multiple characters
ifs command not found
empty ifs bash
bash ifs whitespace
does ifs reset
bash ifs empty string
bash ifs remove newline

I have a function that needs to change IFS for its logic:

my_func() {
  oldIFS=$IFS; IFS=.; var="$1"; arr=($var); IFS=$oldIFS
  # more logic here
}

Can I declare IFS as local IFS inside the function so that I don't need to worry about backing its current value and restore later?

It appears to work as you desire.

#!/bin/bash
changeIFSlocal() {
    local IFS=.
    echo "During local: |$IFS|"
}
changeIFSglobal() {
    IFS=.
    echo "During global: |$IFS|"
}
echo "Before: |$IFS|"
changeIFSlocal
echo "After local: |$IFS|"
changeIFSglobal
echo "After global: |$IFS|"

This prints:

Before: |
|
During local: |.|
After local: |
|
During global: |.|
After global: |.|

Setting IFS for a single statement, (with bash , you can omit the command if not in sh/POSIX emulation). are called anonymous functions and are typically used to set a local scope. with other shells that support As IFS is defined local, changes to it don't affect the global IFS. IFS variable is commonly used with read command, parameter expansions and command substitution. From the bash man page: The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters.

You can designate IFS as a local variable; the local version is still used as the field separator string.

Sometimes it is useful to run a function in a completely isolated environment, where no changes are permanent. (For example, if the function needs to change shell options.) This can be achieved by making the function run in a subshell; just change the {} in the function definition to ():

f() ( 
  shopt -s nullglob
  IFS=.
  # Commands to run in local environment
)

$IFS - Linux Shell Scripting Tutorial - A Beginner's handbook, You can change the value of IFS as per your requirments. The Internal Field Separator (IFS) that is used for word splitting after expansion and to split lines into  If you set a local variable inside the function body with the same name as an existing global variable, it will have precedence over the global variable. Global variables can be changed from within the function. Return Values # Unlike functions in “real” programming languages, Bash functions don’t allow you to return a value when called.

Yes it can be defined!

As long as you define it local, setting of the value in the function does not affect the global IFS value. See the difference between the snippets below

addNumbers () {
    local IFS='+'
    printf "%s\n" "$(( $* ))"
}

when called in command-line as,

addNumbers 1 2 3 4 5 100
115

and doing

nos=(1 2 3 4 5 100)
echo "${nos[*]}"

from the command line. The hexdump on the above echo output wouldn't show the IFS value defined in the function

echo "${nos[*]}" | hexdump -c
0000000   1       2       3       4       5       1   0   0  \n
000000e

See in one of my answers, how I've used the localized IFS to do arithmetic - How can I add numbers in a bash script

Changing IFS in bash function, I have a function in bash that takes arguments. does IFS work in a function or does it IFS is a global, so if you change it inside a function, be sure to change it back before the Try to declare all variables intended for internal use as local. 3. In other programming languages it is common to have arguments passed to the function listed inside the brackets (). In Bash they are there only for decoration and you never put anything inside them. The function definition ( the actual function itself) must appear in the script before any calls to the function.

I got confused because I changed the value of IFS to : inside the function (without using local) and then tried to display the value of IFS with this command, after calling the function:

echo $IFS

which displayed an empty line that made me feel the function wasn't changing IFS. After posting the question, I realized that word splitting was at play and I should have used

echo "$IFS"

or

printf '%s\n' "$IFS"

or, even better

set | grep -w IFS=

to accurately display the IFS value.

Coming back to the main topic of local variables, yes, any variable can be declared as local inside a function to limit the scope, except for variables that have been declared readonly (with readonly or declare -r builtin commands). This includes Bash internal variables like BASH_VERSINFO etc.

From help local:

local: local [option] name[=value] ...
Define local variables.

Create a local variable called NAME, and give it VALUE.  OPTION can
be any option accepted by `declare'.

Local variables can only be used within a function; they are visible
only to the function where they are defined and its children.

Exit Status:
Returns success unless an invalid option is supplied, a variable
assignment error occurs, or the shell is not executing a function.

Bash Functions, A Bash function is essentially a set of commands that can be called Local variables can be declared within the function body with the local Global variables can be changed from within the function. When double quoted, "$*" expands to a single string separated by space (the first character of IFS) - "$1  Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up. Sign up to join this community

Changes, Fixed a bug that could cause an IFS character in a word to result in an extra and `export' to create local array variables when used within shell functions. q. I have a function in bash that takes arguments. does IFS work in a function or does it apply only to the main script? | The UNIX and Linux Forums Changing IFS in bash function The UNIX and Linux Forums

Internal Variables, Bash version info: for n in 0 1 2 3 4 5 do echo "BASH_VERSINFO[$n] $IFS defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse This variable is the line number of the shell script in which this variable appears. Path to binaries, usually /usr/bin/, /usr/X11R6/bin/, /usr/​local/bin, etc. Bash IFS: its Definition, Viewing it and Modifying it Posted on June 22, 2014 August 15, 2015 by Ahmed Amayem When writing a bash shell script it is important to understand how IFS (Internal Field Separtor) works and how it can be modified/changed.

bash - return array from function and display contents, Already answered here. You should do a minimal search in google, because this was the first link returned for "bash return array". Edit: In bash, functions don't  One way to think of it is to imagine that the local var1="local 1" has the effect of saving the current value of var1, with a promise that at the end of the function it will be restored, and then setting it to "local 1". With this mental model you can then think of all variables as global, and variables being restored at the end of functions.

Comments
  • why not change the value of IFS only when calling your function? IFS=. my_func "some arg"
  • I don't want the caller to take that responsibility so that there is no coupling between the caller and the callee.