I have a script that needs to execute many commands, and I want to be able to encapsulate the execution as a function.
The sample functions are as follows
#!/bin/bash
cmd="redis-cli info | grep cluster_enabled"
cmd1="cmd1"
cmd2="cmd2"
exec(){
local cmd=$1
EXEC_RET=$($cmd 2>&1)
EXEC_STATUS=$?
echo "$EXEC_RET"
echo "$EXEC_STATUS"
}
main(){
exec "$cmd"
}
main "$@"
However, the following error is reported after execution
$ bash test.sh
ERR syntax error
0
But if I don't extract the execution command, it won't report an error
exec(){
EXEC_RET=$(redis-cli info | grep cluster_enabled 2>&1)
EXEC_STATUS=$?
echo "$EXEC_RET"
echo "$EXEC_STATUS"
}
main(){
exec
}
main "$@"
# stdout
$ bash temp.sh
cluster_enabled:1
0
What causes this error and how to solve it? I really appreciate any help with this.
execas a function name - that name is already a shell built-in