placing a value in a variable and making it stay - linux

I have been trying to write a script where instead of using names I have done the obvious and saved time by assigning user ids and placed them in a variable. var1=usid
echo $usid shows that its done correct but I save, exit and when I reopen in the morning, the variable no longer exists. Is there a way to make it permanent?

Why don't you use Unix export?
VAR=value
export VAR
The export command will mark each VAR for automatic export to the environment of subsequently executed commands i.e. make the local shell variable VAR global.

Related

Name a function as an environment command

I wonder if there is a way to create a script as following :
#!/bin/ksh
export() {
echo "We're in the local function"
}
export
The main issue is that exportis an environment command for setting value of a variable or an environment variable, so when this script is run it will just prompt something like :
LOGIN=autosys
LOGNAME=autosys
MAIL=/usr/spool/mail/autosys
PWD=/home/autosys/wlp
SHELL=/usr/bin/ksh
TZ=Europe/Paris
HOME=/home/autosys
...
Obviously I could just name my function with a different name but as I am curious (and because this function should really be named export, logic purpose), I wonder if there is a way letting the script know I want to run the local export() function and not the environment command.
For example something like
export(){...}
call function(export)
Yes, export is a reserved word in shell. You will have to choose a different name.
Furthermore, you will have to source your shell script to apply any export to current shell instead of his son, forked.
If you really want to invoke something named “export”, use a shell file named “export”, for instance in /usr/local/bin:
/usr/local/bin/export bimbo lola juliet

Passing and updating an environment variable constantly

I need a way to constantly update a environment variable from a script. I need to use that environment variable in another program almost real time.
What I have is this code:
# !bin/bash
while :
do
AMA="$(cut -c 1-13 text.txt)"
source directory/this_script
done
I am getting the right information from my file when running cut this way. I just need to get this variable permanently updating if possible. Also a way to even get it to the environment.
Slackware Linux

How do I export credentials as environment vars from a shell script?

I've written a shell script, called credentials.sh, which contains:
export USERNAME = 'user'
export PASSWORD = 'pass'
Security of these variables is no big deal.
Two questions.
When I run this script, ./credentials.sh (both as root and not), it doesn't actually export the vars. Why is this?
What is the preferred way to do so they are universally available (Ubuntu 14.04)?
Two changes:
Fix your variable assignment syntax:
export USERNAME='user'
To make this value always available to user, append the above code line to the file ~/.profile; this will set the variables during login, such that this will not need to be manually invoked at other times.

Why forever can't see environment variable?

I had a problem for accessing environment variable using forever.
Thanks to this post, I have a workaround making env vrariable accessible but I'm still wondering why I have to add them all the time :
MY_VARIABLE=SOMETHING forever start my_script.js
because I've previously done an :
export MY_VARIABLE=SOMETHING
Then opened a new terminal but still no luck ...
I would like to set my variable once and not have to write it down each time I'm managing my script ...
Thanks for people who will take time to make me less an idiot and satisfy my curiosity ...
export setting tightly on the current bash or session effectively.It exists in memory.

Child shell does not get empty environment variables and arrays of parent shell

After installation of Ruby Version Manager (RVM) as root on an Ubuntu 14.04. I am confronted with a strange behaviour of bash. Let's have a look at the exported environment variables. I login as user ubunutu and run exportin my bash. Here are three of rvm's exported environment variables, others are available:
declare -ax chpwd_functions='([0]="__rvm_cd_functions_set" [1]="__rvm_after_cd")'
declare -x rvm_version="1.25.28 (stable)"
declare -x rvm_ruby_mode
Everything is pretty fine, but when I run bash -c export we get only:
declare -x rvm_version="1.25.28 (stable)"
Can someone explain me why all empty environment variables and all arrays are removed in the child bash? What must I do to ensure that really all environment variables of a parent shell are available within a child shell?
This problem is really a blocker for me. I am using vagrant and its shell provisioner. In one script I setup rvm and in a second one I must configure some gemsets. The problem is that in the second script the rvm commands do not run. The active shell only gets those environment variables of rvm with are non-arrays and non-empty. Manually sourcing of rvm.sh is no solution!
It is because as per last line of man bash:
Array variables may not (yet) be exported.
I read somewhere a note by BASH developer that it is because exporting an array is very complex and error prone.
Also this line:
declare -x rvm_ruby_mode
is only declaring a name of the variable with export attribute set (without value), if you assign it a value it will be available in the sub shell.
Here is post by BASH author on export of array in BASH.
A variable is created when a value is assigned to a name using the = operator, for example
foo=bar
creates a variable named foo with the value bar.
declare is used for two reasons: one, to allow dynamic creation of variables (which is beyond the scope of this question), and two, to set attributes on names (not necessarily variables). The command
declare -x rvm_ruby_mode
simply sets the export attribute of the name rvm_ruby_mode. To actually create a variable whose name has the export attribute set, you need to use the = operator, just as without the declare command.
declare -x rvm_ruby_mode=
Now rvm_ruby_mode is an empty variable whose named is marked for export.
I say "marked for export" because variables are not exported until a subshell is created. Until then, there is simply a list of names that, if the name has a value when a subshell/child process is created, are copied into the new environment. This list is separate from the list of actual variables (which again are names with associated values).
As to why arrays cannot be exported? The environment is technically not a set of variables, since a variable is a shell construct and the environment is something used by all processes in POSIX, whether or not run by a shell. The environment is simply a list of strings of the form <name>=<value>. There is no standard for how to pack the elements of an array into a single string which any process can parse and reconstruct into an appropriate data structure. While it's possible that bash could make an exception if it new the child process was another bash shell and come up with some way of embedding an array in the environment (like it does with function definitions), apparently this has not been done.

Resources