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

4 Answers

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.

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 

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy