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.
Related videos on Youtube
Author by
user882903
Updated on September 17, 2022Comments
-
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 about 14 yearsAre you using the "C Shell" (
csh
) or the more common Borne / Bash Shell (sh
andbash
respectively)? -
user882903 about 14 years@mctylr: C Shell
-
-
DaveParillo about 14 years+1 good point about not using $path. That would be bad.
-
John T about 14 yearsWell, $PATH is a special variable, $path is not. I'd still avoid using it though.
-
Assaf Levy about 14 yearsOr
~/.bash_profile
or, for system wide effect,/etc/profile
. +1 for mentioning persisting it, in any case. -
njd about 14 yearsYou 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 about 14 yearsThat is for the wrong shell. He wanted csh.
-
Justin Smith about 14 years$path is fine, as mentioned above. And this question is tagged csh, and that syntax is for bash.
-
user882903 almost 14 yearsWhat are the differences between doing a
set dir="/a/b/c/d/e/f"
andsetenv dir "/a/b/c/d/e/f"
? -
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 useset
. Also,set
supports arrays andsetenv
doesn't. -
Dennis Williamson almost 14 yearsYou almost certainly don't need to use
export
. In Bash orsh
,your_path="/a/b/c/d/e/f"
is almost always sufficient.