Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Scripting: Using a variable to define a path

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?

like image 676
Nonameghost Avatar asked Jan 21 '12 04:01

Nonameghost


People also ask

How do I set the PATH variable in bash shell?

For Bash, you simply need to add the line from above, export PATH=$PATH:/place/with/the/file, to the appropriate file that will be read when your shell launches. There are a few different places where you could conceivably set the variable name: potentially in a file called ~/. bash_profile, ~/. bashrc, or ~/.

How is the PATH variable used by the shell?

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user.

How do I include a PATH variable?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.


1 Answers

Don't use spaces...

(Incorrect)

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

(Correct)

SPTH='/home/Foo/Documents/Programs/ShellScripts/Butler' 
like image 94
Web User Avatar answered Sep 22 '22 00:09

Web User