Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-13-2009, 12:40 AM   #1
LQ Newbie
Registered: Nov 2007
Location: Shanghai,China
Distribution: gentoo
Posts: 19
Blog Entries: 1

Rep: Reputation: 0
How to make shell variable don't expand immediately


When the X variable is changed,However the Y variable isn't changed according to X's change,Y already expand when define it.

Is there any way that expand variable when only referring Y variable ,not define it?

Old 06-13-2009, 09:36 AM   #2
Senior Member
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,707

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Uhm.. this is not called expanding I believe. Expanding is when file* becomes file1 file2 file34 etc. IMHO.

As for your problem, Y get the value of $X when you say it has to get it, i.e. Y=$X gives the value of $X to Y, and no matter what happens to X afterwards, Y won't change. It is one of the fundamentals of programming in this type of languages.

In an ordinary programming language you would create a function getY or so which would look at $X at the moment of calling. But Bash doesn't provide function return values, at least not easily.

The only thing you can do is NOT using Y, but always ${X}/tmp when you need this string. You can beautify your program a bit more to define /tmp just once Z="/tmp", so if you have to change this value you'd only have to do it in one place. You reference would become: ${X}/${Z} wherever you use $Y now.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
korn shell variable help 4play Programming 9 11-23-2011 01:03 PM
Shell scripting : how to name a variable with the value of another variable JimOrJoe Programming 9 06-15-2008 10:36 PM
shell variable sniffer_raghav Linux - Newbie 4 06-12-2008 06:34 AM
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 01:49 PM
Variables in Make file do not expand in shell? realos Programming 5 08-07-2005 06:16 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration