4

Consider the following command:

sudo -i bash -c 'A=2; echo "|${A}|"; echo "|$A|";'

When I run this on Debian Linux with bash 4.2.37 and sudo 1.8.5p2 it prints:

|2|
|2|

But when I run it on MacOS High Sierra with bash 3.2.57 and sudo 1.8.17p1 it prints:

|2|
||

Why doesn't $A show without the curly braces?

This doesn't happen if the -i option is omitted -- both 2 are printed. And it doesn't happen if I just run bash without going through sudo.

Ran into this behavior in this answer to a SO question. The answerer there is running Linux with bash 4.3.48 and sudo 1.8.16 and getting the missing output. So it looks like it's related to the sudo version (or maybe something in /etc/sudoers that his Linux and my Mac have in common).

marked as duplicate by ilkkachu bash Jul 10 '18 at 17:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.