0

I have a simple segment of code that works when I run it on it's own, although it doesn't work when it is called from a file.

Code below:

for files in *; do echo ${files::10}; done 

When I try to put this inside a file (e.g. MyScript.sh) I run the following:

sh MyScript.sh

I receive the following error "MyScript.sh: 2: MyScript.sh: Bad substitution"

Why is this happening? How can this be fixed?

Tminer
  • 302
  • 2
  • 4
  • 14

1 Answers1

5

You should call it using bash not sh as you are using bash syntax

bash MyScript.sh

or add a shebang (as the first line in your script)

#! /bin/bash

and set execution permission

chmod +x MyScript.sh

then you can invoke it

./MyScript.sh
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134