3 ms·
How 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!
by aexaey 8y ago
How 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}" ;)
- yjftsjthsd-h 8y agoExcept you should really be in the habit of typing `cat "${today}"` ;) Quote everything!
- LukeShu 8y agoWhy? The {} doesn't prevent glob expansion or field separation.
- _kst_ 8y agoNot as good. I use $today a lot in file and directory names, The overhead of having to type $(today) rather than $today would be, for me, significant. I do have a workaround for this particular case: PROMPT_COMMAND='today=$(printf "%(%F)T\n" -1)' but it only works in bash 4.2 and later. I could use PROMPT_COMMAND='today=$(date +%F)' but I'm trying to avoid executing an external command on every prompt. (Maybe the overhead is low enough that it's not worth worrying about.) My thoughts are (a) if user-defined special variables like this were a shell feature, I could find other uses for them and (b) it seems neater to make such a feature available to users rather than restricting it to three special-case built-in variables. On the other hand, it might have been cleaner for $RANDOM, $EPOCHSECONDS, and $EPOCHREALTIME to be implemented as built-in functions rather than as special variables.
- _kst_ 8y agoI have a better (at least cleaner IMHO) workaround: PROMPT_COMMAND='printf -v today "%(%F)T" -1' printf's "-v" option was added in bash 4.0. printf's "%(...)T" format was added in bash 4.2. The "-1" argument became optional in bash 4.3. So here's what I now have in my .bashrc : if [[ "$BASH_VERSION" > "4.2" ]] ; then PROMPT_COMMAND='printf -v today "%(%F)T" -1' else PROMPT_COMMAND='today=$(date +%F)' fi (The "> 4.2" test is true for bash 4.2 and later, since the value of $BASH_VERSION for 4.2 is "4.2.0(1)-release". The ">" does a string comparison. I'll have to revisit this when and if there's a bash version 10.0, probably using $BASH_VERSINFO.)