4

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~
1
  • 1
    Give SpellCheck a try for simple errors.
    – user234837
    Apr 18, 2014 at 17:05

4 Answers 4

9

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.

5
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
3

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)

1
  • 1
    Quoting doesn't harm, but $() already returns a single token, ie. no quoting is necessary.
    – mike3996
    Apr 18, 2014 at 11:48
0

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.

1
  • Then I shall retract my comment and edit the post.
    – eyoung100
    Apr 17, 2014 at 20:21

You must log in to answer this question.

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