Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a very simple shell script, called deploy.sh

#!/bin/sh 

CDIR= $(pwd)
echo Hello World!

Unfortunately, running it gives the following error:

bird@bird-laptop:~/foo$ sh deploy.sh 
deploy.sh: 3: deploy.sh: /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 deploy.sh
-rw-r--r-- 1 bird bird   327 April  18 00:53 deploy.sh~
share|improve this question
    
Thank you all for the helpful answers!!! –  bird234 Apr 17 at 20:00
1  
Give SpellCheck a try for simple errors. –  BroSlow Apr 18 at 17:05
    
That's an awesome tool, @BroSlow! Thanks! –  bird234 Apr 21 at 10:00

4 Answers 4

up vote 8 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)
CDIR=$(pwd)

Here is a quote from the given link:

=

the assignment operator (no space before and after)

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

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

#!/bin/sh 

CDIR=$(pwd)
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 at 20:21

Your Answer

 
discard

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.