2

I am trying to initialize a variable in a case statement in a bash script,

function cpfiles(){
case $1 in
        a) echo "a" ; source = ${HOME}/dev/a.zip ; dest = 'PROGRA~2\\a.zip';;
        b) echo "b" ; source = ${HOME}/dev/b.zip ; dest = PROGRA~2\\b.zip;;
        *) echo "INVALID MODULE !" ;;
esac

echo ${source} ${dest}
}

but I am getting this error:

[#] cpfiles a
a
bash: =: No such file or directory
bash: dest: command not found...

What am I missing?

1
  • Except of the answers, I would check quotes in b) after dest= Commented Apr 10, 2017 at 9:56

4 Answers 4

3

Your script contains this :

a) echo "a" ; source = ${HOME}/dev/a.zip ; dest = 'PROGRA~2\\a.zip';;
b) echo "b" ; source = ${HOME}/dev/b.zip ; dest = PROGRA~2\\b.zip;;

The problem :

  • source is a shell builtin
  • You added extra spaces before and after the = sign. You may find that easier to read, but it is not valid shell syntax.

So instead of assigning a value to a variable named source, you are actually calling the source builtin, passing it = as an argument.

Try this instead :

a) echo "a" ; source=$HOME/dev/a.zip ; dest='PROGRA~2\a.zip';;
b) echo "b" ; source=$HOME/dev/b.zip ; dest='PROGRA~2\b.zip';;

Please note that the braces around HOME, while perfectly valid, are not required because there is no ambiguity where the variable name ends (/ is not valid in a variable name, so the shell stops there while parsing). Double quoting would be used by most people on the assignments, but it is not required when the assigned string contains no whitespace (even if its expanded value does).

One last issue... In one of the cases you are single-quoting the assigned value for dest, and also escaping the backslash. This will produce a value containing two backslashes, which I assume is not what you want. Remove either the quotes or one of the backslashes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, when I remove the spaces all worked, very helpful
1

Assignments in the shell don't take a whitespace around the =. It's completely valid for some command to expect a single = as an argument.

You get =: No such file or directory because source is a shell command that tries to open the named file:

source: source filename [arguments]
    Execute commands from a file in the current shell.

and dest: command not found because the latter part is taken as running command dest.

Comments

1

the spaces are important in bash, used to split arguments ; to set a variable

source=${HOME}/dev/a.zip

as source is a command the following command is trying to open the file = which doesn't exist

source = ...

Comments

0

Two things important in your script which are causing error:

1. source : Its a shell variable and can be used to load any functions file into the current shell script or a command prompt. It read and execute commands from given FILENAME and return.

2. "=" in Unix a=b without space treated as assignment operator and a = b used for comparison of two strings or you can check in condition statement like [[ $str1 == $str2 ]. It is an alternative method for string equality check.

Also $HOME will be enough to fetch the value of variable instead of ${HOME}, but if you are using it it won't through any error. See the below context.

*$ echo ${SHELL}

/bin/bash

$ echo $SHELL

/bin/bash*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.