Cameron
Cameron

Reputation: 2965

expr command not found? Why is expr not found but everything else is?

I have spent several hours trying to make the following piece of code work

    PATH="C:\Ben\MyPictures"
    echo $PATH
    MY=`expr 2 + 2`

but this will not work because "expr: command not found". The only thing I've dug on StackOverflow are pathing issue (I.E. set my environment variable), but if that's the problem, why would other functions like echo, let, and declare already work fine?

For more context, this is on a near-fresh installation of window's cygwin. My question is why can't I find expr?

Upvotes: 2

Views: 6733

Answers (2)

riteshtch
riteshtch

Reputation: 8769

You have modified your PATH to have only 1 directory(therefore it cant find expr). You must append your new path to PATH and not replace existing PATH values, like this:

export PATH="$PATH:C:\Ben\MyPictures"

Also instead of calling an external process expr for calculation you can use the bash's builtin arithmetic evaluation:

$ echo $((2+2))
4

Edit:

Yes those would work because they are not executable files found from directories listed in $PATH.

Instead they(echo, type etc) are functionality provided by the bash shell itself called shell built-ins.

Type out type echo and type expr to know what type of command is it(alias/shell builtin/executable file etc.)

Shell built-ins help can be usually found out by help shellBuiltin where as we use man pages for executable files.

PS: type itself is a shell built-in(see type type)

Upvotes: 7

sjsam
sjsam

Reputation: 21965

The path variable PATH is used to find the location of standard binaries that ship with your environment. In fact system environment variables are usually capitalized( for example HOME ).

In your command :

PATH="C:\Ben\MyPictures"

You have accidentally(or intentionally?) replaced the standard paths to the standard binaries by C:\Ben\MyPictures so that command interpreter, bash here, can no longer find expr. When you use capitalized variables,say PATH, you could do:

if [ -z "$PATH" ] #check if a variable is empty
then
PATH="C:\Ben\MyPictures"
else
PATH="$PATH:C:\Ben\MyPictures"
fi

The better option indeed is to use small letters for user defined variables :

path="C:\Ben\MyPictures"
#in this case you should not get an error for expr but
# I am not sure how a Linux-Like environment handle character case.
# check that out.

Upvotes: 3

Related Questions