[Bash] Backticks, xargs and Arithmetic

Backticks

Using to exec cmd and return the output as string

$ echo `date`
Fri Jun 7 15:40:11 EEST 2024

The same effect you can achieve by using

$ echo $(date)
Fri Jun 7 15:40:11 EEST 2024

Examples

$ echo `date +%F`
2024-06-07

$ echo some log data > blah-`date +%F`.log
# it creates log files blah-2024-06-07.log

arithmetic

With $((...)) expressions, you can do simple arithmetic on the command line.

$ echo $((4*5+1))
21

$ echo Greetings from the year $((`date +%Y`+1000))
Greetings from the year 3024

xargs

Read from stdin, and apply the result to next cmd, kind of like point free sytle.

$ date +%Y | xargs echo Grettings from the year
Greetings from the year 2024
posted @ 2024-06-07 20:47  Zhentiw  阅读(2)  评论(0编辑  收藏  举报