Gregor Isack
Gregor Isack

Reputation: 1131

Using while loop without process substitution, while allowing global variable value modification inside the loop

I have a script that when I run from a file manager (Filza), it return an error saying

command substitution: syntax error near unexpected token `('.
line 56: `paste -d'\n' <(echo "$var1") <(echo "$var2"))'

But when I run it from a terminal (./myscript.sh), it ran with no error. Here's the code that gave the error:

#!/bin/bash

var1="A
B
C"
var2="1
2
3"
globalvar=0

while read v1 && read v2; do
    globalvar=$(echo $v1 $v2)
done<<<$(paste -d'\n' <(echo "$var1") <(echo "$var2"))

As commented below, it's probably some shell doesn't allow process substitution, thus why it failed. This command is running on iOS environment (jailbroken). Is there alternative way to implement this? Thanks in advance!

Upvotes: 0

Views: 59

Answers (1)

dash-o
dash-o

Reputation: 14452

Try using 'here document' (<<) instead of 'here string' (<<<). It is supported by most shells.

while read v1 && read v2; do
    globalvar=$(echo $v1 $v2)
done <<__END__
$(paste -d'\n' <(echo "$var1") <(echo "$var2"))
__END__

The other option is create a shell wrapper that will force bash (from the question, looks like bash is installed and working). Rename original script to script-run, and modify the shell script to call the script-run

#! /bin/sh
exec /bin/bash ${0%/*}/script-run "$@"

Or other equivalent.

Upvotes: 1

Related Questions