Normally, we use
sh script.sh 1>t.log 2>t.err
sh script.sh $string
You need to use 'eval' shell builtin for this purpose. As per man page of
eval [arg ...] The args are read and concatenated together into a single command. This command is then read and exe‐ cuted by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.
Run your command like below:
eval sh script.sh $string
However, do you really need to run
sh command? If you instead put
sh interpreter line (using
#!/bin/sh as the first line in your shell script) in your script itself and give it execute permission, that would let you access return code of
ls command. Below is an example of using
sh and not using
sh. Notice the difference in exit codes.
Note: I had only one file
try.sh in my current directory. So
ls command was bound to exit with return code 2.
$ ls try1.sh try1.sh.backup 1>out.txt 2>err.txt $ echo $? 2 $ eval sh ls try1.sh try1.sh.backup 1>out.txt 2>err.txt $ echo $? 127
In the second case, the exit code is of
sh shell. In first case, the exit code is of
ls command. You need to make cautious choice depending on your needs.