LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-18-2014, 01:37 AM   #1
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Rep: Reputation: Disabled
dynamic variable contents $() ?


Hi all,

I am new to linux and learning to use PS1. It came to me that if i set
PS1=`date` , the date is always static. Meaning it doesnt reflect the current date.

Code:
Thu Dec 18 15:30:01 SGT 2014 >PS1=" `date` > "
 Thu Dec 18 15:30:03 SGT 2014 > 
 Thu Dec 18 15:30:03 SGT 2014 > 
 Thu Dec 18 15:30:03 SGT 2014 > 
 Thu Dec 18 15:30:03 SGT 2014 >
If i set it to $(date), it is still the same.

Quote:
Thu Dec 18 15:30:03 SGT 2014 > PS1=" $(date) > "
Thu Dec 18 15:30:42 SGT 2014 >
Thu Dec 18 15:30:42 SGT 2014 >
Thu Dec 18 15:30:42 SGT 2014 >
Thu Dec 18 15:30:42 SGT 2014 >
Thu Dec 18 15:30:42 SGT 2014 >
But if i add a backslash "\" infront, the dates are updated dynamically.

Quote:
Thu Dec 18 15:35:54 SGT 2014 > PS1=" \$(date) > "
Thu Dec 18 15:35:58 SGT 2014 >
Thu Dec 18 15:35:59 SGT 2014 >
Thu Dec 18 15:36:00 SGT 2014 >
Thu Dec 18 15:36:02 SGT 2014 >
q1) what does $() means ? is it the same meaning as `` ?
q2) why adding a \ works ? what does \ means then in this scenario?

Regards,
Noob
 
Old 12-18-2014, 02:24 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
$() is the modern replacement for `` but in addition it allows additional features such as nesting which can be extremely difficult if not impossible at times using ``

As for why escaping (placing a slosh (\) prior to a character) allows it to be more dynamic, you are setting the prompt to the string '$(date)' which is then evaluated by the shell each time you hit enter.

There are escape sequences you can use to do the same ... here is the one I use at work currently:
Code:
PS1='\[\e[1;33m\][\D{%d-%m-%Y %H:%M.%S}] \[\e[1;32m\]\w\n\[\e[1;36m\][\u.\h]\[\e[0m\]$ '
 
Old 12-18-2014, 02:55 AM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
a) not that much different but backticks have a slight different treatment for backslashes and some other special characters than $()
though they does the same when provided with something to execute.
$() can be easily nested while backticks to be nested takes to escape likes of them to be read as backticks.

POSIX has a good info.

b) PS1 itself in bashrc executes something listed inside it and since without escaping the $ it doesnt know it would need to run the date command again it stored the one time output as if read at startup.

if i were incorrect anywhere above, someone should come and correct.
 
Old 12-18-2014, 01:00 PM   #4
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
Hi Grail and sabhi, thanks for replying.


Quote:
Originally Posted by grail View Post
$() is the modern replacement for `` but in addition it allows additional features such as nesting which can be extremely difficult if not impossible at times using ``

As for why escaping (placing a slosh (\) prior to a character) allows it to be more dynamic, you are setting the prompt to the string '$(date)' which is then evaluated by the shell each time you hit enter.

There are escape sequences you can use to do the same ... here is the one I use at work currently:
Code:
PS1='\[\e[1;33m\][\D{%d-%m-%Y %H:%M.%S}] \[\e[1;32m\]\w\n\[\e[1;36m\][\u.\h]\[\e[0m\]$ '

Hi Grail, yes i am setting the prompt (PS1) to the string $(date), but if i dont put a \, it is only evaulated once. But if i put a \, it is evaluated everytime.

What does the "\" means then ? $(date) \$(date)
Does it means to instruct the shell to evaluate everytime instead of 1 time ?

Regards
Noob
 
Old 12-18-2014, 01:11 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,463

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
Quote:
Originally Posted by szejiekoh View Post
Hi Grail and sabhi, thanks for replying.





Hi Grail, yes i am setting the prompt (PS1) to the string $(date), but if i dont put a \, it is only evaulated once. But if i put a \, it is evaluated everytime.

What does the "\" means then ? $(date) \$(date)
Does it means to instruct the shell to evaluate everytime instead of 1 time ?

Regards
Noob
If you don't use the backslash, then you aren't setting PS1 to the string "$(date)", you're setting PS1 to the OUTPUT of the command $(date) when you ran the command. The $(date) is being executed first, and the result is set to PS1. So when you run export PS1="$(date)", the ACTUAL command is export PS1="Thu Dec 18 12:07:29 MST 2014" (or whatever your current date is). It's easiest to see the difference if you just echo the contents of PS1.

Code:
$ export PS1="$(date) > "
Thu Dec 18 12:07:29 MST 2014 > echo $PS1
Thu Dec 18 12:07:29 MST 2014 >
Thu Dec 18 12:07:29 MST 2014 > export PS1="\$(date) > "
Thu Dec 18 12:08:01 MST 2014 > echo $PS1
$(date) >
See the difference? Without the backslash, $(date) gets evaluated at the time you set PS1. So PS1 is not set to $(date), it's set to the literal string "Thu Dec 18 12:07:29 MST 2014" (or whatever your current date/time happened to be). With the backslash, the $(date) command is not evaluated until PS1 is used, rather than when it's set. You can accomplish the same thing by using single quotes instead of double:
Code:
export PS1='$(date) > '
Like with the backslash, variables or functions inside single quotes don't get evaluated until they're used. Similar to:

Code:
$ a=5
$ b=$a
$ echo $b
5
$ b='$a'
$ echo $b
$a
$ eval echo $b
5
People get into the same kind of trouble with ssh:
Code:
$ ssh server "echo $HOSTNAME"
client
$ ssh server 'echo $HOSTNAME'
server
With double quotes, the variable $HOSTNAME is expanded before ssh is even called, so your ssh command is actually "ssh server echo client". With single quotes (or if you had put a backslash in front), the literal string "$HOSTNAME" is sent through ssh, where it's evaluated on the server.

When you start nesting commands or variables like this, you need to pay close attention to WHEN and WHERE your command is actually being executed (or when/where your variable expansion is actually taking place, as the case may be).

Last edited by suicidaleggroll; 12-18-2014 at 01:21 PM.
 
1 members found this post helpful.
Old 12-19-2014, 01:31 AM   #6
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
If you don't use the backslash, then you aren't setting PS1 to the string "$(date)", you're setting PS1 to the OUTPUT of the command $(date) when you ran the command. The $(date) is being executed first, and the result is set to PS1. So when you run export PS1="$(date)", the ACTUAL command is export PS1="Thu Dec 18 12:07:29 MST 2014" (or whatever your current date is). It's easiest to see the difference if you just echo the contents of PS1.

Code:
$ export PS1="$(date) > "
Thu Dec 18 12:07:29 MST 2014 > echo $PS1
Thu Dec 18 12:07:29 MST 2014 >
Thu Dec 18 12:07:29 MST 2014 > export PS1="\$(date) > "
Thu Dec 18 12:08:01 MST 2014 > echo $PS1
$(date) >
See the difference? Without the backslash, $(date) gets evaluated at the time you set PS1. So PS1 is not set to $(date), it's set to the literal string "Thu Dec 18 12:07:29 MST 2014" (or whatever your current date/time happened to be). With the backslash, the $(date) command is not evaluated until PS1 is used, rather than when it's set. You can accomplish the same thing by using single quotes instead of double:
Code:
export PS1='$(date) > '
Like with the backslash, variables or functions inside single quotes don't get evaluated until they're used. Similar to:

Code:
$ a=5
$ b=$a
$ echo $b
5
$ b='$a'
$ echo $b
$a
$ eval echo $b
5
People get into the same kind of trouble with ssh:
Code:
$ ssh server "echo $HOSTNAME"
client
$ ssh server 'echo $HOSTNAME'
server
With double quotes, the variable $HOSTNAME is expanded before ssh is even called, so your ssh command is actually "ssh server echo client". With single quotes (or if you had put a backslash in front), the literal string "$HOSTNAME" is sent through ssh, where it's evaluated on the server.

When you start nesting commands or variables like this, you need to pay close attention to WHEN and WHERE your command is actually being executed (or when/where your variable expansion is actually taking place, as the case may be).
Hi suicidal,

Thanks. Appreciate the detailed explanation.

Regards,
Noob
 
Old 12-19-2014, 01:49 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,498

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Please mark as SOLVED once you have a solution / answer
 
Old 12-19-2014, 01:52 AM   #8
acoustee3012
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Rep: Reputation: Disabled
Nice job, tks u bro
 
Old 12-20-2014, 01:36 PM   #9
szejiekoh
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Please mark as SOLVED once you have a solution / answer

Noted Grail. Done that already..

Thanks.
 
  


Reply


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
Export variable as contents of another variable jnojr Linux - Newbie 3 05-08-2012 02:48 PM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM
same system variable, but with different contents? kpachopoulos Linux - General 1 07-31-2005 05:15 PM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 01:23 PM


All times are GMT -5. The time now is 10:17 PM.

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