Question about enviroment variable.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about enviroment variable.
# 1  
Old 06-23-2015
Question about enviroment variable.

Hi Gurus,

I am stuck on below issue.
in my .profile. I have two variable:
Code:
 
x=abc$123 
t=xyz$

when running env command, I got below:
Code:
 
x=abc
t=xyz$

my OS is SunOS 5.10 sun4v sparc SUNW,SPARC-Enterprise-T5220

I am wondering why t=xyz$ shows exactly value?

I try below:
Code:
 
par1=xyz
export par1
par2=123$par1
export par2

I got below:
Code:
 
par1=xyz
par2=123xyz.

thanks in advance
# 2  
Old 06-23-2015
Code:
x=abc$123 
t=xyz$

x gets the assignment value of the string abc and the dereference of the variable $123, which if it is not set it would be undefined.

t gets the assignment value of the string xyz$. The `$' is not being interpreted by the shell as the dereference of a variable, since it exist as the last character. It is the equivalent of 'xyz$' or xyz\$
This User Gave Thanks to Aia For This Post:
# 3  
Old 06-23-2015
ken6503,
You haven't said which shell you're using.

I have forgotten how /bin/sh will expand $123 but I thought that:
Code:
x=abc$123

would assign x the string abc followed by the contents of the 1st positional parameter followed by the string 23. I don't know why you didn't get the 23? If you were trying to set x to the literal string abc$123, you could do that with any of the following:
Code:
x=abc"$"123
x='abc$123'
x=abc\$123

or millions of variations of any of those three possibilities.

There is nothing special about a dollar sign at the end of a string (unless the string ends with $$, so the code:
Code:
t=xyz$
echo "$t"

should produce the output:
Code:
xyz$

I can't imagine that any shell, when given the commands:
Code:
par1=xyz
export par1
par2=123$par1
export par2

will produce the period you showed us at the end of the output you reported:
Code:
par1=xyz
par2=123xyz.

Aia,
No, $123 does not expand to the value of the 123rd positional parameter; it expands to the value of the 1st positional parameter followed by the string 23. To get the 123rd positional parameter with a POSIX conforming shell, you would need to use ${123}. With a pure Bourne shell you can't reference any positional parameters after the 9th positional parameter directly without shifting away lower positional parameters.

And, expanding an undefined variable (including positional parameters) produces an empty string; not some undefined value.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 06-23-2015
Quote:
Originally Posted by Don Cragun
Aia,
No, $123 does not expand to the value of the 123rd positional parameter; it expands to the value of the 1st positional parameter followed by the string 23. To get the 123rd positional parameter with a POSIX conforming shell, you would need to use ${123}. With a pure Bourne shell you can't reference any positional parameters after the 9th positional parameter directly without shifting away lower positional parameters.

And, expanding an undefined variable (including positional parameters) produces an empty string; not some undefined value.
Don,
Thank you for correcting that, somehow I forgot that normal variable names start with letters or underscore, thus my premises were wrong, since I was considering abc$123 as the shell trying to interpret abc$var
Quote:
which if it is not set it would be undefined
Just bad wording. I meant the variable does not exist. It is undeclared. The variable itself, not the value it can produce.

Last edited by Aia; 06-23-2015 at 09:29 PM.. Reason: Grammar
# 5  
Old 06-23-2015
Quote:
Originally Posted by Don Cragun
ken6503,
You haven't said which shell you're using.

I have forgotten how /bin/sh will expand $123 but I thought that:
Code:
x=abc$123

would assign x the string abc followed by the contents of the 1st positional parameter followed by the string 23. I don't know why you didn't get the 23? If you were trying to set x to the literal string abc$123, you could do that with any of the following:
Code:
x=abc"$"123
x='abc$123'
x=abc\$123

or millions of variations of any of those three possibilities.

There is nothing special about a dollar sign at the end of a string (unless the string ends with $$, so the code:
Code:
t=xyz$
echo "$t"

should produce the output:
Code:
xyz$

I can't imagine that any shell, when given the commands:
Code:
par1=xyz
export par1
par2=123$par1
export par2

will produce the period you showed us at the end of the output you reported:
Code:
par1=xyz
par2=123xyz.

Aia,
No, $123 does not expand to the value of the 123rd positional parameter; it expands to the value of the 1st positional parameter followed by the string 23. To get the 123rd positional parameter with a POSIX conforming shell, you would need to use ${123}. With a pure Bourne shell you can't reference any positional parameters after the 9th positional parameter directly without shifting away lower positional parameters.

And, expanding an undefined variable (including positional parameters) produces an empty string; not some undefined value.
Thanks Don for your reply. My OS is Solaris.

shell is ksh.
please see attached screenshot.
Question about enviroment variable.-unixvarjpg
# 6  
Old 06-23-2015
Quote:
Originally Posted by ken6503
Thanks Don for your reply. My OS is Solaris.

shell is ksh.
please see attached screenshot.
And the thumbnail shows exactly what I said you should get for the things you showed us.

You said you got:
par2=123xyz.
I said the period shouldn't be there. The period is not there in the screenshot.

You didn't show us in that screenshot what happens with the following commands:
Code:
x=abc$123
echo "$x"

You said you got:
abc
I said that instead, it should have produced:
abcsome(possibly empty)string23
where some(possibly empty)string should be the same thing you get from:
printf '%s\n' "$1"
# 7  
Old 06-24-2015
Quote:
Originally Posted by Don Cragun
And the thumbnail shows exactly what I said you should get for the things you showed us.

You said you got:
par2=123xyz.
I said the period shouldn't be there. The period is not there in the screenshot.

You didn't show us in that screenshot what happens with the following commands:
Code:
x=abc$123
echo "$x"

You said you got:
abc
I said that instead, it should have produced:
abcsome(possibly empty)string23
where some(possibly empty)string should be the same thing you get from:
printf '%s\n' "$1"
Thanks Don for your reply.
Sorry, I misunderstood you. the period is type.

when I try
Code:
par2=abc$123
export par2

I got
HTML Code:
abc23
when trying
Code:
part=abc$xyz
export par2

I got
Code:
abc

could you please give me a brief explanation about this? thanks.
Question about enviroment variable.-unixvar1jpg
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing all the process of a particular enviroment

Hi Folks, I have the below command that will kill all the process of an environment, lets say if I have reached to the location cont directory under which I want to kill multiple process so the command will be .... kill -9 `ps -ef | grep cont | grep -v grep | awk '{print $2}'` Now please... (4 Replies)
Discussion started by: punpun66
4 Replies

2. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

3. Shell Programming and Scripting

How to pass enviroment variable from csh to Informix sql script

Hello, I have a csh script that creates an environment variable. I want to pass the environment variable(CURR_TABLE_DATE) to an Informix sql script. Here is the csh: #!/bin/csh -f setenv INFORMIXSERVER market3_tcp setenv CURR_TABLE_DATE 20090714 set DATABASE = gm_cdr set SQL_DIR =... (0 Replies)
Discussion started by: jwoj
0 Replies

4. Solaris

Save enviroment variables

I need to save my enviroment variables,specially the $PATH.When I put it on .cshrc at next reboot I lost the configuration.How can avoid this?Thanks (2 Replies)
Discussion started by: bgf0
2 Replies

5. Shell Programming and Scripting

enviroment settings

What are the environment setting during a cron session? I have HP-UX and I want to send the output/file from a script to several e-mail addresses. I want to create an env-var to store the e-mail addresses in my .profile, but I do not know if it will be visible when a script is executed in a cron. (4 Replies)
Discussion started by: ALTRUNVRSOFLN
4 Replies

6. Shell Programming and Scripting

Enviroment Differences script(s)

Im looking for any sample scripts that would output a current environment setup to a file and possibly then take 2 files and list any differences This will allow us to quickly see any differences between environments in case of issue? At a high level this would take Unix Kernel Params,... (2 Replies)
Discussion started by: JoeShmoe
2 Replies

7. Solaris

set enviroment variable..

Hello... I was wondering can anyone explain me how to set up enviroment variable to be permanent... I tryed with setenv but my solaris does not have this command... then I did: export ORACLE_SID=base1 export ORACLE_BASE=/home export ORACLE_HOME=$ORACLE_BASE/oracle/8.1.6 and by... (5 Replies)
Discussion started by: amon
5 Replies

8. UNIX for Dummies Questions & Answers

Enviroment variables...

Hi guys, thanks in advance for this easy answer.... :s Ok I am trying to output the enviroment varable for host in Solaris. I have tried $HOST, $HOST_NAME, $HOSTNAME carn't find it anywhere, does someone want to put me out of my misary and tell me what it is??? :confused: :eek: Thanks... (2 Replies)
Discussion started by: B14speedfreak
2 Replies

9. UNIX for Dummies Questions & Answers

Accessing remote machine via an enviroment variable

Hi. Is it possible to remotely access another unix box via an enviroment variable, on another machine? I am trying to create an environment variable $MIPSDATA which will point to a folder on another machine. I have setup the .rhosts file and got that working on both machines (tested via a... (4 Replies)
Discussion started by: ElCaito
4 Replies

10. UNIX for Dummies Questions & Answers

Help! - How do I compile C++ in UNIX Enviroment?

:confused: Hi, does anyone here know how to compile and run C++ in UNIX environment? I am so desperate! Any help is greatly appreciated. Thanks! (3 Replies)
Discussion started by: Kahuashi
3 Replies
Login or Register to Ask a Question