Unix Bourne-shell environment variable - unix

I want to create a .profile file that will have a welcome message everything I log in. However, I need to set the environment variable which I don't understand. Are you able to tell me what it does and how to do it?

To display all environment variables and their values:
env
To change or set an environment variable:
export var=value
To remove an environment variable:
unset var
Typically, a welcome message can be shown by customizing the /etc/motd file to your liking. If you want, you can also add some messages in /etc/profile by using the echo or print commands.
For example:
echo "Welcome ${USER}"
or
echo "Welcome $(whoami)"

Code:
$ VAR=Hello
$ echo $VAR
Output:
Hello
Ref link.

Related

when trying to create file in unix which has $ in that file it is throwing a number?

trying to create a file with following data using shell script.
InsertParam.sh
echo "$$Domain=XYZ" >parameter.prm
when i run InsertParam.sh
Am getting out put as
$cat parameter.prm
1979205Domain=XYZ
Please help me how to over come this in my parameter.prm
i need Data as
$$Domain=xyz
In sh/bash/ksh/zsh, $$ is the current PID. see https://www.gnu.org/software/bash/manual/bashref.html#Special-Parameters
You need to use different quotes to prevent that variable from being expanded:
echo '$$Domain=XYZ' >parameter.prm
see https://www.gnu.org/software/bash/manual/bashref.html#Quoting
Quotes can be mixed, as required:
echo '$$Domain='"$domain" >parameter.prm

Assigning commands into variable

What is the right way to assign commands into variable . a.k.a how should i use quote commas and how assign it to variable , so the command is up to date? For example
i am at home/desktop
i assign variable
b=`pwd`
echo "$b" // home/desktop
but when i change the directory
cd games
pwd // home/desktop/games
echo "$b" // home/desktop
it does not update. Also i want to do something like
g="-l"
ls $g
is it possible?
Variables never update automatically, you have to re-assign them, i.e.,
b=`pwd`
echo "$b" // home/desktop
cd games
pwd // home/desktop/games
b=`pwd`
echo "$b" // home/desktop/games
as for your second question, I think that's only possible using eval:
g=" -la"
eval "ls"$g
BE VERY CAREFUL WITH THIS It's very easy to write something that can compromise security and indeed most people (including myself!) would strongly advise you never to use eval unless all else has failed.
Consider the code above for demonstration purposes only, under no circumstances to be used in a production system.
I think you are confusing assigning variables with aliases.
assigning a variable means you store the result of the command in a variable
what this command does
b=`pwd`
is running pwd and stores the answer in a variable b.
and alias means giving some command a different name. so running alias b pwd will make it so whenever you run b, you will actually run pwd
b is only set when you run the first command (assigning the output of pwd to b, it doesn't link those items together (so that b gets set on every invocation). There are ways to do that also (do man bash on your machine) (for example, the shell variable $PWD contains this information.
to your second question, yes, you can do that, but a nice way to so it is to use alias. So, alias ll='ls -l' creates a command on your system that will run ls -l when you type ll

Behavior of ECHO Command and Variable Usage in Unix

Can't post the image directly yet, so here's a link to it:
http://i.imgur.com/sjPVNU6.png
I'm trying to learn some basic unix file scripting for an Informatica project I'm working on. I've used a few scripts in the past and have only a very rudimentary idea of what to do so I'm just playing around with the various parts of an existing script.
I can't make heads or tails of some of the behavior of the commands. Here is an example using ECHO. The thing appears to just randomly return one of: the variable I defined, the variable name, or an error.
e.g. why does:
$ _Src="/home/axxxxxx"
$ echo "${_Src}"
return
/home/axxxxxx
but
$ INFA_HOME="/home"
$ echo "${INFA_HOME}"
returns
ksh: "${INFA_HOME_^H}": bad substitution
other inscrutable behavior:
$ echo "${INFA_HOME} now"
>
$ echo "${INFA_HOME} now"
/home now
$ echo "${INFA_HOME} "
{INFA_HOME}
$ echo "${INFA_HOME} now"
/home now
Looks like your terminal has some issues. Make sure echo $TERM agrees with the emulation mode of the terminal, and that locales etc. are correctly set up.
I think you should check your putty or terminal settings. You may see in the image below the string works as expected.
http://i.stack.imgur.com/5CQ1j.png
the screenshot that you shared shows "^H" which is equivalent to backspace, so the chances you might have used that. The next time you use the variable it prints the output as expected.
Even with the first variable all goes fine.

How do I keep functions/variables local to my zshrc?

Any variable that I declare in my zshrc is available in the shell as an environment variable. I don't want this to happen.
I tried putting the variables in a function and setting them as local, but then the function is available outside of the zshrc.
How can I make it so what happens in my zshrc stays in my zshrc?
If you're using a recent version of zsh you can use an anonymous function:
function () {
local xyz=abc
# whatever
}
The function will be automatically executed and then thrown away, it exists only for scoping purposes.
This works for any sourced file, not only zshrc.
They are available, but they are not exported so scripts launching from command-line don’t get these variables. If your .zshrc looks like
function zshrc()
{
local VAR=1
# Do stuff
}
zshrc
and you then never want to launch zshrc function again you can just do
unfunction zshrc
afterwards.
If you do not prefix a variable with the word local it will remain until you do one of the following:
Open a new terminal window.
Run exec zsh or exec bash depending on your shell. This just clears out your local variables that were not assigned with the word local.
Avoid this
method_name(){
a=11
echo $a
}
Correct Example
method_name(){
local a=11
echo $a
}
This variable is scoped to the function name method_name and only available inside of the function when called (and not afterwards).
If you want direct access to that local variable you can set it this way
local z=11
And call it this way
echo $z
Additionally, environment variables are different from local variables
Depending on your shell and needs, you may use .bash_profile or .bashrc or .zshrc etc. to store functions and aliases.
View this reference for more on environment variables and their respective shells
Also read this to understand how to set environment variables on the command line using shell expansions
You can quickly view environment variables with env or printenv
The convention is to use UPPERCASE
To temporarily set an environment variable (stored until you close the terminal)
export A=11 or export B="11 is part of this string"
Assuming you have opened a new terminal window or sourced .zshrc or .bashrc or whichever you are using you can now use this environment variable until you close your terminal session. Note: do not use $ when setting, but do use $ when referencing the variable.
Examples
echo "A is equal to: $A and that is pretty nice"
echo "$A"
How to source a file
source ~/path/to/file/filename
Example
source ~/.bash_profile
To set an environment variable (until you remove it or set it again)
Use the code above but place it in your ~/.bash_profile or ~/.zshrc or other respective file. Save the file and source it.
Example
export B="11 is part of this string"
You now can view it with
env
To remove that environment variable, remove it from the file and again source the file.
To temporarily remove an environmental variable, use unset
Example
unset B
Note there is no $ when unsetting.
To set environment variables from the command line
export BLABLA="environment variable set from the command line, saved in file for later use"
Check the file you are sending it to, it may not start on a new line, it might have been concatenated to your last line which was some other function, alias or other.
This is not a fully comprehensive answer, but it is a great step in the right direction. It shows how scope in a terminal shell can be set, used and removed.
There is apparently a bash convention to name 'private' functions with double underscore .. of course they are not actually private . I am using this convention in my .zshrc.
function __comment()
{
curr=`pwd`
echo "$curr $*"
}
__comment 'Here is a Comment'

Setting environment variable in shell script does not make it visible to the shell

I want to use a shell script that I can call to set some environment variables. However, after the execution of the script, I don't see the environment variable using "printenv" in bash.
Here is my script:
#!/bin/bash
echo "Hello!"
export MYVAR=boubou
echo "After setting MYVAR!"
When I do "./test.sh", I see:
Hello!
After setting MYVAR!
When I do "printenv MYVAR", I see nothing.
Can you tell me what I'm doing wrong?
This is how environment variables work. Every process has a copy of the environment. Any changes that the process makes to its copy propagate to the process's children. They do not, however, propagate to the process's parent.
One way to get around this is by using the source command:
source ./test.sh
or
. ./test.sh
(the two forms are synonymous).
When you do this, instead of running the script in a sub-shell, bash will execute each command in the script as if it were typed at the prompt.
Another alternative would be to have the script print the variables you want to set, with echo export VAR=value and do eval "$(./test.sh)" in your main shell. This is the approach used by various programs [e.g. resize, dircolors] that provide environment variables to set.
This only works if the script has no other output (or if any other output appears on stderr, with >&2)

Resources