set not working as I expect

openSUSE 12.3
KDE 4.10.3
Konsole

I simply cannot get what I expect from the set command. I have looked at the man page till my eyes cross. I looked at the command reference book with the same result. Please tell me where I’m going wrong.

As I understand,

set MyVar "Hello World"

will create an environment variable named MyVar that contains the string Hello World.

If I then,

set MyVar

I should see the string Hello World.

I can use the contents of that variable in a script by addressing it as $MyVar

It doesn’t work this way. Using konsole, when I set the variable,

set MyVar "Hello World"

I get the prompt back without any indications. I expected this.
When I do the

set MyVar

I just get the prompt without any indiactions that the variable exists.

If I do

set > result.txt

I get a file that seems to hold all the environment variables, but MyVar is not shown among them.

Obviously, I’m not understanding the situation properly. Sometimes a person get things stuck in their head and it’s hard to see the problem.

I did notice that the ah… system variables seem to all be upper case. Is it good policy to use all lower or mixed case to avoid overwriting something important?
If I ever do get to set a variable, will it survive the closing and reopening of konsole?
Will it disappear if I log out?

Bart

What shell are you using?

For “bash” (and “sh”) it is


MyVar="Hello World"

which creates a shell variable (not an environment variable). To make it an environment variable, you also need


export MyVar

Here “export” implies exporting into the environment.

A shell variable is for one instance of the shell. An environment variable is available to all commands called from that shell.

You can combine the above with


export MyVar="Hello World"

If you are using c-shell (“csh”) or “tcsh”, then


set MyVar="Hello World"

works for a local shell variable, while


setenv MyVar "Hello World"

will work for an environment variable.

It is easy
Just assign a value to variable

$MyVar="Hello World"

you can check the value stored inside variable using echo. Ensure that you are using $ to indicate that you are referring to a variable when using echo
correct way

$echo $MyVar
Hello World

wrong way

$echo Myvar
Myvar

Using your set command i can find the variable using grep

$set |grep MyVar
MyVar='Hello World'
$

OK! I guess my problem was not understanding that there is a difference in the various shells.

I always kinda wondered about that. :\

Thanks!

Bart

Oops! Guess I spoke too soon.


bart@Asus-990FX:~> date +%y-%m-%d
13-05-14

This is what I want. I’m going to make a directory with this string as the name.
The application I’m going to call stores it’s output in a directory that is specified as a parameter. It creates that directory if it does not exist.
So, I put it into a variable that I can call it later.
example: application -d ~/appdir/$MyVar


bart@Asus-990FX:~> MyVar= date +%y-%m-%d
13-05-14
bart@Asus-990FX:~> echo $MyVar

bart@Asus-990FX:~> 

Huh? That didn’t work like I was expecting.
Let’s try,


bart@Asus-990FX:~> date +%y-%m-%d >MyVar
bart@Asus-990FX:~> echo $MyVar

bart@Asus-990FX:~> 

That didn’t work either.

Let’s try appending the date string to the directory string:


bart@Asus-990FX:~>MyVar= "appdir/"date +%y-%m-%d
bash: appdir/date: No such file or directory
bart@Asus-990FX:~> 

Hmm…


bart@Asus-990FX:~> MyVar= "appdir/"
bash: appdir/: Is a directory
bart@Asus-990FX:~> 

No! It’s not a directory! I put quote marks around it! That makes it simply a string!

I am trying to write a script, correction, a bash script, that will take one parameter and, using an if - elif statement, select one of several predefined strings, putting that string into a variable. Then build another string from the date and put that into another variable, and finally call an application using the result from the if statement as the first parameter, and the date as the second parameter. The frustrating thing is that I could do this in about 10 minutes if I were using MS DOS! It’s a lot harder to have to forget stuff before learning stuff than it is to simply learn! That last little disagreement with this computer showed me that I need an example to get me kick started!

Can someone cobble up a pattern and I’ll see if I can flesh it out.

Bart

I did not read through all you have written down there, but at the first few lines, I guess you want to have the current date (as returned by the date utility) put into a variable. That is done with

MyVar=$(date +%y-%m-%d)

You realy should try to teach yourself some basic shell (partculary bash) knowledge. Because while people here are willing to help you when you are stuck somewhere now and then, that is probably not the case if you are stuck at evry other character, showing a complete lack of knowledge about bash.