In terminal:
VAR="Extremely long and often used command"
echo $VAR
Output:
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?
In terminal:
VAR="Extremely long and often used command"
echo $VAR
Output:
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?
You can put it in your .bash_profile
, which gets executed every time 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"
.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
.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 07:32
You can create/modify/delete permanent variables using kv-bash
functions:
1) Download kv-bash
file from github:
git clone https://github.com/damphat/kv-bash.git
cp -ar ./kv-bash/kv-bash /usr/local
chmod +x /usr/local/kv-bash
2) Import kv-bash functions:
# You can also put this line in .bash_profile
source kv-bash
3) Now create/modify variables
#let try create/modify/delete variable
kvset myEmail john@example.com
kvset myCommand "Very Long Long Long String"
#read the varible
kvget myEmail
#you can also use in another script with $(kvget myEmail)
echo $(kvget myEmail)
#delete variable
kvdel myEmail
I learned it from this https://hub.docker.com/r/cuongdd1/cloud-provisioning-packs/~/dockerfile/
.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