Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In terminal:

VAR="Extremely long and often used command"
echo $VAR


Extremely long and often used command

So far it works fine, but after restarting a terminal my variable doesn't exist. How to fix it?

share|improve this question
The general rule is if it's a variable that is only useful to the shell, don't bother exporting it and just put it in .bashrc. If it's an environment variable put it in .profile, and you'll want to export it too. That should work. There's no way to know why it doesn't for you unless you provide more info (do you do anything weird with your rc files, etc.) – jw013 Nov 16 '12 at 16:04
Why on earth would one do anything weird with or in an rc file? I'm shocked that such things go on. – Bruce Ediger Nov 16 '12 at 16:37
@BruceEdiger care to explain why you feel so strong about 'weird' things in an rc file? – jippie Nov 16 '12 at 17:43
Regarding the new edit: see my first comment. Also, don't misuse variables: variables are not for storing full commands. Use a function or script instead. – jw013 Nov 16 '12 at 18:16
@jippie: I think BruceEdiger is joking. – dubiousjim Nov 16 '12 at 19:49
up vote 8 down vote accepted

You can put it in your .bash_profile, which gets executed everytime you log in.

Or if it is an alias for a long command, you can put this in your .bash_aliases file under your home directory:

alias short_version="very long command here"
share|improve this answer
If you have your aliases in .bash_aliases, you must source that file (with source or .) in .bashrc (or otherwise) for it to work - it's not done automatically, at least not for me. – Emanuel Berg Nov 16 '12 at 22:34
@EmanuelBerg, some distros (notably Ubuntu) provide a default .bashrc that automatically sources .bash_aliases if it exists. But you're right, it's not a standard Bash startup file. – cjm Nov 19 '12 at 7:32
@cjm: Aha, so that's it. I've heard many people talk about that file so I suspected it was if not a standard, then at least a common practice, from somewhere. Ubuntu. – Emanuel Berg Nov 19 '12 at 20:00

Your Answer


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.