I have a loop in a bash script, test.sh
that reads as follows:
#!/bin/bash CHOSEN_NQUEUE=0 foo(){ for chunk in $(seq 0 $((${CHOSEN_NQUEUE}-1))); do echo "CHUNK = $(($chunk+1))" done } bar(){ CHOSEN_NQUEUE=10 foo } bar
This loop has previously been working fine up till now. If I run the program as . test.sh
, I get the following error code in the loop:
-bash: 0 1 2 3 4 5 6 7 8 9+1: syntax error in expression (error token is "1 2 3 4 5 6 7 8 9+1")
If I run the program as bash test.sh
, then the function produces the desired result:
CHUNK = 1 CHUNK = 2 CHUNK = 3 CHUNK = 4 CHUNK = 5 CHUNK = 6 CHUNK = 7 CHUNK = 8 CHUNK = 9 CHUNK = 10
This is a snippet from a much larger program; if I run this program with bash program.sh
, the error I see in the first case persists.
Particularly, if I simply run foo
, then the error does not occur. If I run foo
from bar
, then the error occurs. This occurs irrespective of using bash program.sh
or . program.sh
.
Can someone kindly suggest what I might be doing wrong? Is it poor practice to run functions from inside other functions in bash?
Kindest regards!
EDIT:
Thanks to everyone in the comments!
Upon realizing this problem arises from using select for arrays, I attempted the following code:
select opt in "${options[@]}" do next=false local IFS=@ case "@${options[*]}@" in (*"@$opt@"*) foo (*) echo "Invalid option: $REPLY" ;; esac echo "" done echo "IFS = $IFS"
The problem arises from IFS=@
, which should not be @ outside of the loop.
However, if I run this code attempting to set IFS
locally, i.e. local IFS=@
, it appears the global IFS
is modified. The code outputs:
IFS = @
Does anyone have an idea why this might be?
Kind regards again!
seq
, i.e.for chunk in "$( seq ... )"
. Could you double check that you are showing us the code that you are running?IFS
is in the working and the non-working case. Put something likeprintf "IFS: %q\n" "$IFS"
in front of thefor
loopIFS: @
in the non working case and IFS: $' \t\n' in the working case