Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Why does the following code not work?

#!/bin/sh
PATH=$PWD/Info
defaults read $PATH

Everytime I execute it I get

./script: line 3: defaults: command not found

Once I do it in one line it work

defaults read $PWD/Info

It's ok like that. I'm just curious.

share|improve this question
    
Variables are case sensitive. By convention, all-caps variables are globals or environment variables. You should use lowercase names for your script-local variables. It's also worth learning about the more important ones among these globals, like PATH. –  mkaito Jun 4 '12 at 14:35
    
@mkaito: there are no custom variables in Hedge code. PATH and PWD must be uppercased to get their special meaning. –  jlliagre Jun 4 '12 at 14:56

2 Answers 2

up vote 4 down vote accepted

Because you are changing $PATH, which is a special variable. The shell uses PATH for searching executable programs. defaults is probably one of them, but after removing its folder from PATH, the shell cannot execute it anymore.

share|improve this answer

Add to the path instead of replacing it:

PATH=$PWD/Info:$PATH
share|improve this answer
    
Should be: PATH="$PWD/Info:$PATH" or PATH="$PATH:$PWD/Info" –  jlliagre Jun 4 '12 at 14:09
1  
@jlliagre: The quotes aren't necessary. Word splitting isn't performed on the right side of an assignment. The only time you would need quotes is if you're including special characters literally. –  Dennis Williamson Jun 4 '12 at 14:22
1  
Indeed, my points were more the separator (you used a semicolon instead of a colon, fixed now by grawity) and the path component order. Unless commands are specifically designed to take precedence over standard one, I prefer the extra components to be appended, not prepended, for obvious security reasons. –  jlliagre Jun 4 '12 at 14:54

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.