Let's check the following script:
#!/bin/sh
func()
{
echo "var: $var";
}
var="val" func
echo "after executing the command, var: $var"
Its output is:
var: val
after executing the command, var: val
The var variable is left set after executing the command.
Is it correct behavior? I thought that command scope variables are set just for a single command.
If I use bash (set #!/bin/bash in the first line) instead of sh then setting a variable for a command doesn't affect the rest of the script:
var: val
after executing the command, var:
In the busybox implementation sh and bash work equally: the variable is left assigned after executing the command.
To workaround the problem I use parentheses when executing such commands:
(var="val" func)
This way the var variable isn't touched in all shells.
/bin/sh. – muru Mar 16 '18 at 03:35