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.

For example there is a long path that I cd to very often. How do I store the path in a variable so that I can use it everytime?

For example: I wan to be able to do this

cd $path

instead of

cd /a/b/c/d/e/f 

everytime.

share|improve this question
1  
Are you using the "C Shell" (csh) or the more common Borne / Bash Shell (sh and bash respectively)? –  mctylr Mar 9 '10 at 21:25
    
@mctylr: C Shell –  Lazer Mar 10 '10 at 3:51

6 Answers 6

up vote 6 down vote accepted

assuming you really want csh/tcsh syntax (as you have tagged your question), put this

setenv P1 "/a/b/c/d/e/f"

to your .tcshrc

after that you are able to do

cd $P1
share|improve this answer

In Bash shell:

export FOO="/a/b/c"

And you don't want to use $path. That's a special variable.

share|improve this answer
2  
+1 good point about not using $path. That would be bad. –  DaveParillo Mar 9 '10 at 15:48
10  
Well, $PATH is a special variable, $path is not. I'd still avoid using it though. –  John T Mar 9 '10 at 15:56
2  
You probably don't need export: just "foo=/a/b/c" would be enough, if you only need to refer to this variable in the current shell. If you want the variable propagated to child processes though (like when you run other programs from the shell), then you'd need the export command. –  njd Mar 9 '10 at 16:39
    
$path is fine, as mentioned above. And this question is tagged csh, and that syntax is for bash. –  Justin Smith Mar 9 '10 at 19:42

Use export.

export your_path="/a/b/c/d/e/f"

cd $your_path

If you want it to persist through logins, you're going to need to edit it into your .profile file.

share|improve this answer
1  
Or ~/.bash_profile or, for system wide effect, /etc/profile. +1 for mentioning persisting it, in any case. –  Jonik Mar 9 '10 at 16:03
1  
That is for the wrong shell. He wanted csh. –  Justin Smith Mar 9 '10 at 19:42
    
You almost certainly don't need to use export. In Bash or sh, your_path="/a/b/c/d/e/f" is almost always sufficient. –  Dennis Williamson Jul 22 '10 at 15:17

It's not likely that you need your variable in the environment.

So, in csh instead of setenv, you can do:

set dir="/a/b/c/d/e/f"
cd $dir

or in Bash, instead of export:

dir="/a/b/c/d/e/f"
cd $dir
share|improve this answer
    
What are the differences between doing a set dir="/a/b/c/d/e/f" and setenv dir "/a/b/c/d/e/f"? –  Lazer Jul 22 '10 at 14:14
    
@Lazer: setenv exports variables so they are available in child processes. set sets variables that will only be used in the current environment (script or interactive shell). Most of the time, you only need to use set. Also, set supports arrays and setenv doesn't. –  Dennis Williamson Jul 22 '10 at 15:15

For csh you probably want to use cdpath . For bash, use CDPATH instead.

For example (bash):

prompt$ export CDPATH=:/a/b/c/d/e

prompt$ cd f
cd /a/b/c/d/e/f

You can also add more colon delimited directory targets. Keep the leading colon so CDPATH checks your current working directory first!

share|improve this answer

If you just want to use the path for one session, set the variable as usual

set long="/some/long/path/to/a/directory"

You can then cd "$long" as often as you like until the shell terminates or you set long again.

If you're interested in the variable being available to processes run from the shell session you should set it in your environment

setenv long "/some/long/path/to/a/directory"

If what you want is for the variable to be available to every session, instead of just the current one, you will need to set it in your shell run control.

$EDITOR ~/.cshrc

Then add the set line or the setenv line shown above to automatically set the variable or environment variable for every session of csh.

share|improve this answer

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.