simple assigning variable question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple assigning variable question
# 1  
Old 06-19-2009
simple assigning variable question

I know many will probably say go read some tutorials...I would like to say that I am. However, I need to know this for work and it needs to be done soon.

In my script I have a while loop that reads a .tbl file of 4 columns and assigns them to variables a, b, c, d.

Once in the loop i do

echo "$a"

and it works but if i do

echo "$a" %prints a
newvar = "$a"
echo "newvar" %prints echo "" (i.e. empty quotes)


I'm sure its some simple beginner syntax for reassigning variables so if someone could let me know how to fix this i'd appreciate it.
# 2  
Old 06-19-2009
Code:
newvar = "$a"

should not have spaces on either side of the = sign.
# 3  
Old 06-19-2009
Bah, yeah i know it doesn't have them in my code. There is also a $ in front of newvar that I forgot to put when i posted this. It's just habit for me to put the space Smilie

Is this correct or am i missing something?

echo "$a" .......prints a
newvar=$a
echo "$newvar" .....prints ""

also if i try and make it a file path it doesn't print
i.e.

home/a/b/c/$newvar......prints home/a/b/c/

Somehow something isn't assigned.

I'd love to show the code but I don't have it at home. However, this is exactly what i have. Guess maybe i'll ask monday if no one sees anyting else obvious.
# 4  
Old 06-19-2009
Works for me:
Code:
$ a=a
$ echo $a
a
$ newvar=$a
$ echo $newvar
a
$

What shell are you using?
# 5  
Old 06-19-2009
It looks like you already noticed the two things I would have mentioned. You did not mention what shell you are using. I've tested it with bash and it seems fine. Here is my code and output.

Code:
#!/bin/bash
a=ABC
echo "$a"
newvar=$a
echo "$newvar"

output:
ABC
ABC

Can you provide the actual code and output you are using?

- B
# 6  
Old 06-19-2009
Bourne, or SH i believe.
# 7  
Old 06-19-2009
This same code works fine for Bourne shell also. Can you post your code and output?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning a variable

I have a date column as 06302015 but I need to have variable which extracts 063015. Am trying something like below but it is not assigning Please let me know if am missing something. Thanks in advance. ################################ #!/usr/bin/ksh DT=06302015 ... (7 Replies)
Discussion started by: weknowd
7 Replies

2. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

3. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

4. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

5. Shell Programming and Scripting

"Simple" echo/reading variable question...

Hello, I have a simple(I think) question! Although simple, I have been unable to resolve it, so I hope someone can help! OK, here it is: 1)I have an awk script that prints something, such as: awk '{print $2}' a > x so x might hold the value of say '10' 2)Now, I just want to check to see if... (4 Replies)
Discussion started by: astropi
4 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. Shell Programming and Scripting

Assigning value to a variable

Is there any difference between: set variable=39 and variable=39 (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

Assigning value to a variable

can we make a global variable and store character values and add other values to that variable ?? for example a="hello, John" and can we add value ". How are you? so a can have "hello, John. How are you?" can someone help me?? (2 Replies)
Discussion started by: bonosungho
2 Replies

9. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies

10. UNIX for Dummies Questions & Answers

Very simple question about changing PS1 variable at startup!

Hello there ! I am new in this Unix world and just start learning Unix. I have very simple question about changing PS1 variable (Shell Prompt) i have local.profile file in my working directory, i open in vi edit mode and add this line PS1="Hello:>" and i save that file. I disconnected from... (2 Replies)
Discussion started by: abidmalik
2 Replies
Login or Register to Ask a Question