How to execute a bash script

Hi All,

I am trying to write a simple bash script.

rm -f File1

I saved this as test.sh
Now when I want to execute it, I type

./test.sh

It gives me error "command not found"

What I am doing incorrect here? Do I have to add anything in script like

#!/bin/bash

Yes, you should add "#!/bin/bash" as the first line.

You also need to set the script executable.

chmod +x test.sh

And, you should never, ever name a script "test" because there's already system utilities named "test".

Thanks for your reply.
I did as mentioned by you.

Now my script looks like:

#!/bin/bash
rm -f File1

I renamed it to Trial12.sh
I also changed mode by using

chmod +x Trial12.sh

And I executed using

 ./Trial12.sh

I got this error:
-bash: ./Trial12.sh: /bin/bash^M: bad interpreter: No such file or directory

Stop editing your scripts with Notepad. Windows saves carriage returns to the end of lines, breaking everything. Use a real editor inside your terminal, like nano or vim.

1 Like

Thanks!
Actually I am new to Unix scripting and not very comfortable with Vi editors.
Now I did everything in unix and it is working.

Thanks Again.

vi was designed for an ancient, minimal, and very alien keyboard, and emacs was designed for something even weirder... I've been scripting for years and still dont' use vi unless I have to. It's useful to learn since lots of unix systems have one or the other and nothing else, but if you install nano, I think you'll find it a lot more familiar.