Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I've written my first bash script:

echo 'hello world!'

I know it has the right location to bash and is executable:

$ which bash
$ chmod +x

Now I want to run it from the command line, but I get an error:

$ command not found

So instead I try this and it works:

$ bash
hello world!

Is this how I will always need to execute it? I feel like I have executed other scripts without having to precede it with bash. How can I run without having to precede it with bash?

Update: Here is a good explanation of why and how to execute a bash script.

share|improve this question
up vote 27 down vote accepted

You have to make the file executable. You can do that with

chmod +x <filename>

where is the name of your script and then you have to prepend it with ./ to instruct the shell to run a file in the local directory, like:


You can only run files that are in your PATH or that you specify a path to them. ./, the local directory, is not in the PATH by default because someone may use it for nefarious purposes. Imagine a script called ls dropped in a directory, you go inside that directory, run ls and that script does something bad.

While you are at it you may want to make it more portable by running shell instead of bash by using:


or by running bash no matter where it is installed as long as it is installed:

#!/usr/bin/env bash
share|improve this answer
I have already done that with chmod +x Did I do it wrong? – Andrew Dec 12 '09 at 19:28
You are probably missing ./ I've added it to the answer. – Pablo Dec 12 '09 at 19:30
Makes sense. And it works. Thanks! – Andrew Dec 12 '09 at 19:34

In addition to the advice by Fernández, precede it with a point, like this:


For security reasons the current directory is never included in the execution path.

share|improve this answer

There are 3 ways to execute bash script. With example as the script you want to execute.

Method 1

execute the script as a sub process, which will trigger permission problems if not the write owner. You can change permissions by chmod.

$ chmod +x

$ ./

Method 2

run the script as a current user. if the bash script is well written, it should not have errors.

$ bash

Method 3

similar to method 2 but a short cut

$ . myscript

hope this helps!:)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .