Bash problem with local IFS variable being ignored under Slackware Current
In Bash, I set up an array of strings, e.g.:
Code:
declare -a X=( a b c d )
I then call a function that needs to copy the array values into a string, with the vertical bar as the separator. Under Slackware 14.2, the following script demonstrates what I'm attempting to do:
Code:
#!/bin/bash
function test_internal_field_separator()
{
local IFS='|'
local STRING_OF_ARGUMENTS="${@}"
echo "${STRING_OF_ARGUMENTS}"
}
declare -a X=( a b c d )
test_internal_field_separator "${X[@]}"
The “echo” command within the “test_internal_field_separator” function will produce the following output:
Under Slackware Current, however, the local IFS variable value appears to be ignored, and the array values are separated by spaces instead:
I have no idea which behaviour should be considered right, but I find it highly unlikely that they are supposed to be different.
In any case, once I diagnosed the problem, I modified my code as follows in order to work around it:
Code:
#!/bin/bash
function test_internal_field_separator()
{
local STRING_OF_ARGUMENTS="${@}"
STRING_OF_ARGUMENTS="${STRING_OF_ARGUMENTS// /|}"
echo "${STRING_OF_ARGUMENTS}"
}
declare -a X=( a b c d )
test_internal_field_separator "${X[@]}"
Hence, the issue is no longer particularly critical, even though it violates the POLA ( “Principle Of Least Astonishment”) and as such, it likely deserves at least a closer look. :confused:
Just for completeness, I ran the following commands to obtain the kernel and bash versions of both systems:
Code:
cat '/etc/slackware-version'
uname -srv
bash --version
Under Slackware 14.2, I get:
Code:
Slackware 14.2
Linux 4.4.157 #2 SMP Fri Sep 21 00:36:59 CDT 2018
GNU bash, version 4.3.48(1)-release (x86_64-slackware-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Under Slackware Current, the output is as follows:
Code:
Slackware 14.2+
Linux 4.19.12 #1 SMP Fri Dec 21 21:11:12 CST 2018
GNU bash, version 4.4.23(1)-release (x86_64-slackware-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
|