58

My problem lies with my confusion with shell variables.

To my understanding, variables allow me to store a value (String in this case) and to call it later in my code. So if I wanted to have a variable that holds the path to some set of scripts, I could ideally just store it like this:

SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'

//Later on in the script//
cd $SPTH
./script1

What I'm trying to do, with probably the wrong syntax, is to set the path to variable SPTH.

Then I use cd with argument $SPTH.

Ideally this would allow me to run the file there without typing in the path. However it doesn't work. The $SPTH is ignored and the result is as if cd was used alone.

So what am I doing wrong? And what would be a way to do this?

1
  • 5
    If I remember correctly, you don't use spaces surrounding the "=". Commented Jan 21, 2012 at 4:38

2 Answers 2

128

Don't use spaces...

(Incorrect)

SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'

(Correct)

SPTH='/home/Foo/Documents/Programs/ShellScripts/Butler'
Sign up to request clarification or add additional context in comments.

3 Comments

what if there is a space in the path
@Eatdoku when using SPTH in your script and its value has a space, then surround the variable with double quotes. E.g. cd "$SPTH"
Can this be run without the need for CD? I was thinking command $SPTH but that seems somehow risky. Maybe I need to variable the path, but hardcode the script name? command $SPTH/somescript.sh ?
7

To add to the above correct answer :- For my case in shell, this code worked (working on sqoop)

ROOT_PATH="path/to/the/folder"
--options-file  $ROOT_PATH/query.txt

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.