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` > " Quote:
Quote:
q2) why adding a \ works ? what does \ means then in this scenario? Regards, Noob |
$() 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\]$ ' |
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. |
Hi Grail and sabhi, thanks for replying.
Quote:
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 |
Quote:
Code:
$ export PS1="$(date) > " Code:
export PS1='$(date) > ' Code:
$ a=5 Code:
$ ssh server "echo $HOSTNAME" 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). |
Quote:
Thanks. Appreciate the detailed explanation. Regards, Noob |
Please mark as SOLVED once you have a solution / answer
|
Nice job, tks u bro
|
Quote:
Noted Grail. Done that already.. Thanks. |
All times are GMT -5. The time now is 04:42 AM. |