How do I set $ variables in unix?

27,388

Solution 1

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

Solution 2

In Bash shell:

export FOO="/a/b/c"

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

Solution 3

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

Solution 4

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:
27,388

Related videos on Youtube

user882903
Author by

user882903

Updated on September 17, 2022

Comments

  • user882903
    user882903 over 1 year

    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.

    • mctylr
      mctylr about 14 years
      Are you using the "C Shell" (csh) or the more common Borne / Bash Shell (sh and bash respectively)?
    • user882903
      user882903 about 14 years
      @mctylr: C Shell
  • DaveParillo
    DaveParillo about 14 years
    +1 good point about not using $path. That would be bad.
  • John T
    John T about 14 years
    Well, $PATH is a special variable, $path is not. I'd still avoid using it though.
  • Assaf Levy
    Assaf Levy about 14 years
    Or ~/.bash_profile or, for system wide effect, /etc/profile. +1 for mentioning persisting it, in any case.
  • njd
    njd about 14 years
    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.
  • Justin Smith
    Justin Smith about 14 years
    That is for the wrong shell. He wanted csh.
  • Justin Smith
    Justin Smith about 14 years
    $path is fine, as mentioned above. And this question is tagged csh, and that syntax is for bash.
  • user882903
    user882903 almost 14 years
    What are the differences between doing a set dir="/a/b/c/d/e/f" and setenv dir "/a/b/c/d/e/f"?
  • Dennis Williamson
    Dennis Williamson almost 14 years
    @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
    Dennis Williamson almost 14 years
    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.