It is very interesting that if you intend to display 0_1 with Bash using the code
x=0
y=1
echo "$x_$y"
then it will only display
1
I tried echo "$x\_$y" and it doesn’t work.
How can I echo the form $x_$y? I’m going to use it on a file name string.
Because variable names are allowed to have underscores in them, the command:
is trying to echo
${x_}(which is probably empty in your case) followed by${y}. The reason for this is because parameter expansion is a greedy operation – it will take as many legal characters as possible after the$to form a variable name.The relevant part of the
bashmanpage states:Hence, the solution is to ensure that the
_is not treated as part of the first variable, which can be done with:I tend to do all my bash variables like this, even standalone ones like:
since it’s more explicit, and I’ve been bitten so many times in the past 🙂