Saturday, September 5, 2015

How to handle nested backticks (`) in Bash

If you use nested backticks (`),

you will get the following errors:

$ echo `printf '%02d' $((10#`date +%M` / 30 * 30))`
-bash: command substitution: line 1: unexpected EOF while looking for matching `)'
-bash: command substitution: line 2: syntax error: unexpected end of file
-bash: command substitution: line 1: syntax error near unexpected token `)'
-bash: command substitution: line 1: ` / 30 * 30))'
date +%M
$

Use $() instead as follows:

$ echo `printf '%02d' $((10#$(date +%M) / 30 * 30))`
00
$

Reference:
http://stackoverflow.com/questions/2657012/how-to-properly-nest-bash-backticks

No comments:

Post a Comment