5 ms·
With this release, bash now has three built-in variables (um, I mean "parameters") whose values are updated every time they're read: $RANDOM yields a random in
by _kst_ 8y ago
With this release, bash now has three built-in variables (um, I mean "parameters") whose values are updated every time they're read:
$RANDOM yields a random integer in the range 0..32767. (This feature was already there.)
$EPOCHSECONDS yields the whole number of seconds since the epoch.
$EPOCHREALTIME yields the number of seconds since the epoch with microsecond precision.
I'm thinking of a new shell feature that would allow the user to define similar variables. For example, I have $today set to the current date in YYYY-MM-DD format, and I have to jump through some minor hoops to keep it up to date.
Does anyone else think this would be useful enough to propose as a new bash feature? Would it create any potential security holes? Should variables like $PATH be exempted?
(Of course this doesn't add any new functionality, since I could use "$(date +%F)" in place of "$today". It's just a bit of syntactic sugar.)
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]
- justizin 8y ago$RANDOM is new in bash 5.0? i'm curious. This has been documented for ages, afaik. headtilt
- dualbus 8y agoYou mean like Ksh's discipline functions? dualbus@system76-pc:~$ ksh -c 'date=; date.get() { .sh.value=$(date +%s); }; echo $date; sleep 5; echo $date' 1546926637 1546926642 See: https://docs.oracle.com/cd/E36784_01/html/E36870/ksh-1.html https://docs.oracle.com/cd/E36784_01/html/E36870/ksh-1.html ("Discipline Functions")
- _kst_ 8y agoYes, just like that!
- scarejunba 8y agoWhat is the advantage of this technique over a shell built-in or function that echoes that value?
- aexaey 8y agoHow does function sound for a syntactic sugar? $ today() { date +%F; } $ echo Today, $(today) is a great day! Today, 2019-01-08 is a great day!
- flukus 8y agoIt's not as nice, "cat $today" is easier to type than "cat $(today)" and would give better completion, just declared matching variables instead of matching functions, files and executables. On the plus side, TIL the subshell syntax plays well with eval/expand shortcut (ctrl+alt+e).
- Vogtinator 8y agoIt's not obvious that just expanding a variable runs code though.
- hawski 8y agoWouldn't "cat $today" result with "cat: No such file or directory: 2019-01-08"? Did you mean echo instead of cat?
- forty 8y agoOnly in the case when no such file exists in the current directory :)
- flukus 8y agoMy real life use case for these dynamic variables would be more like "cat/vim/cp $log" to get today's log file which would expand to something like /somedir/logs/201901/09/product.log. Handy when you have a large matrix of products/environments.
- edoloughlin 8y ago* "cat $today" is easier to type than "cat $(today)"* Except you should really be in the habit of typing "cat ${today}" ;)
- swini 8y agoYou can already do this interactively with PROMPT_COMMAND, e.g. PROMPT_COMMAND='date=$(date +%D);time=$(date +%T)'
- BGZq7 8y agoNot quite - that is run before your prompt is displayed. $ PROMPT_COMMAND='time=$(date +%T)' $ echo $time;date +%T 12:19:07 12:20:19 Thus it will show the time after your last command returned rather than the current time.
- mitgraduate 8y agoHere is my favourite bash script: https://blog.ashfame.com/2018/02/deploying-lambdas-multiple-regions/ https://blog.ashfame.com/2018/02/deploying-lambdas-multiple-... Deploy a lambda function in multiple regions (15 regions!) with just one bash script using Apex up. Add route53 on top with Latency Based Routing enabled and you've a latency in 10s of millsecond from anywhere on the globe without paying a hefty fee for this kind of latency.
- hendry 8y agoLove Apex Up
- hobofan 8y agoHow is that in any way related to the parent comment?
- mitgraduate 8y agoIt's not my blog, i thought mentioning a script in bash would be a good idea.
- nothrabannosir 8y agoPlease no. The parallel universe Me where I didn't happen to read this random thread would never know, consequently never expect random variables to do that. And curse you forever if I found out while banging my head debugging some buggy script.
- IshKebab 8y agoYeah what's wrong with functions? Clearly the Bash developers are continuing their tradition of obeying the principle of most surprise.
- john-aj 8y agoJudging from this thread, Bash doesn’t seem to be the only shell to support these types of variables, but I agree – it’s unintuitive and surprising.
- ducktective 8y agoAnyone knows why a `microsecond since epoch` is named "realtime"? What is so "guaranteed" about it?
- iofiiiiiiiii 8y agoAnd does it even count leap seconds? Regular Unix timestamps do not (time stands still during leap seconds)
- okdana 8y agobash's EPOCHREALTIME was inspired by mksh, which was in turn inspired by zsh. And in zsh, EPOCHREALTIME was named after the system real-time clock (specifically the CLOCK_REALTIME clock ID passed to clock_gettime()). And i imagine that was named by analogy with hardware real-time clocks, since they both track human ('real') time. But i don't know much about electronics or the history of POSIX clock sources.
- hkt 8y agoSyntactic sugar is a phrase permanently marred for me by the ruby community: it means "cognitive load for other people who know the language but don't keep up with the faddy bits". It made me tremendously sad.
- wernsey 8y agoNicely put. I'm reminded of the Jargon file that says "Syntactic sugar causes cancer of the semicolon."
- palunon 8y agoYeah, but C-style "for" loops are nice, right ? Still, they really are a syntactic sugar for a "while". The problem is more with a language having fady bits and people using them when they don't make the code clearer, than with syntactic sugar.
- hkt 8y agoSure, I agree actually - if you are going to have a language that has lots of nice syntax, _let it be built in from the start_. Or only released at major increments, etc etc.
- loa-in-backup 8y agoHopefully Bash 6.0 then
- yesenadam 8y agoC-style "for" loops are nice, right Well... I know macros are supposed to be bad, but.. I've been programming in C for a long time, but only recently tried 'sugaring' my loops with #define _(x,y) for (x=0;x<(y);x++) ..which has worked soo well, I'm sticking with it. It makes programming in C much more pleasurable. (My programs mostly have a lot of nested loops over multidimensional arrays, mostly from 0, inc'ed by 1) So now instead of e.g. for (i=0;i<WID;i++) for (j=0;j<HEI;j++) { it's just _(i,WID) _(j,HEI) { which makes me smile every time.