Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

I have a very simple shell script, called


CDIR= $(pwd)
echo Hello World!

Unfortunately, running it gives the following error:

bird@bird-laptop:~/foo$ sh 3: /home/bird/foo: Permission denied
Hello World!

Any clues, why this is happening?

bird@bird-laptop:~/foo$ ls -l
total 156
-rwxrwxrwx 1 bird bird   327 April  18 00:57
-rw-r--r-- 1 bird bird   327 April  18 00:53
share|improve this question
Thank you all for the helpful answers!!! – Dilini Apr 17 '14 at 20:00
Give SpellCheck a try for simple errors. – BroSlow Apr 18 '14 at 17:05
That's an awesome tool, @BroSlow! Thanks! – Dilini Apr 21 '14 at 10:00
up vote 9 down vote accepted

Remove the space character after the = in

CDIR= $(pwd)

sh doesn't allow any spaces around the =.

CDIR= $(pwd) means "Run the output of pwd with the variable CDIR unset."

As you can't run a folder you get Permission denied.

share|improve this answer
CDIR= $(pwd)
# ...^

You must not have spaces around the = in an assignment.

What this is doing: var=value command is a legal statement. It sets the "var" variable in the environment of the "command", but only in that environment. For your command, the shell sees this:

CDIR= $(pwd)           # first, process the $()
CDIR= /home/bird/foo   # prepare the env with CDIR="" and execute /home/bird/foo
                       # oops, cannot execute /home/bird/foo
share|improve this answer

When you assign a value to a variable in sh (or other shell scripting language), never use any space before and after equal:

CDIR= $(pwd)

Here is a quote from the given link:


the assignment operator (no space before and after)

share|improve this answer
Quoting doesn't harm, but $() already returns a single token, ie. no quoting is necessary. – progo Apr 18 '14 at 11:48

Florian is correct, but you're still missing an update


echo $CDIR
echo Hello World!

After the value is assigned it must be converted to a string, as you cannot inline a command directly to a variable for display. See here.

share|improve this answer
Then I shall retract my comment and edit the post. – eyoung100 Apr 17 '14 at 20:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.