5

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?

jw013
  • 50,274
  • 9
  • 137
  • 141
0x6B6F77616C74
  • 183
  • 1
  • 1
  • 7
  • 5
    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. –  Nov 16 '12 at 16:37
  • 1
    @BruceEdiger care to explain why you feel so strong about 'weird' things in an rc file? – jippie Nov 16 '12 at 17:43
  • @0x6B6F77616C74 please explain what you want to use the variable for and why it is important that it survives a reboot. – jippie Nov 16 '12 at 17:46
  • 4
    Regarding the new edit: see my first comment. Also, don't misuse variables: [variables are not for storing full commands](http://mywiki.wooledge.org/BashFAQ/050). Use a [function or script](http://unix.stackexchange.com/a/30964/9537) instead. – jw013 Nov 16 '12 at 18:16
  • Reverted edit: in this context label doesn't mean anything – jw013 Nov 16 '12 at 18:52
  • 1
    @jippie: I think BruceEdiger is joking. – dubiousjim Nov 16 '12 at 19:49
  • @dubiousjim me and my big feet :-( – jippie Nov 17 '12 at 07:16
  • @jw013, can you please post that as an answer? Then we can [take advantage of the stack exchange engine](http://meta.unix.stackexchange.com/a/3927/135943) and propose edits, comment on *your* answer, etc. I think it's a decent one.... ;) – Wildcard Mar 16 '16 at 00:53

2 Answers2

9

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"
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
MelBurslan
  • 6,836
  • 2
  • 24
  • 35
  • 1
    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
  • 1
    @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 07: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
2

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 [email protected]
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/

user219911
  • 21
  • 1
  • Just letting people know there is a re-written fork of this with far more features. https://github.com/imyller/kv-sh Has dump, import, restore, exists and the ability for a local and fallback (ro) default database. – Shanness Aug 16 '20 at 06:49