Question

When i run the following script:

#!/bin/bash 
test="abcdr"
test2=${test:0:2}
echo $test2

I get the error: script: 3: script: Bad substitution

But then I type exactly the same commands in the terminal it works fine without any error

Why is that happening and how can I fix it?

Was it helpful?

Solution

You are running your script with sh, which is not the same as bash. It doesn't support this type of substitution. Run your script with

bash script

or make sure it's executable (chmod u+x /path/to/script) and run it directly

/path/to/script

If your current directory happens to be /path/to, you can just type

./script

OTHER TIPS

@user3597432 i don't see any issue in that script content, mostly the issue might be with bash version or how the script being executed. if you couldn't resolve by bash terminal, you can try korn shell if possible which is more convenient. just refer the ksh bin and script file to .ksh. ksh provides debug option execute with ksh -x script.ksh.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top