BASH - printf printing escape codes even though I have %0b defined
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH - printf printing escape codes even though I have %0b defined
I have a dynamic list that I want it to print up to 250 entries in 5 grouped columns of 50; each 'group; has a number and the name of a server. I also want the number to be colored but the name column to not be colored. My code works great until I hit null characters within the array, then it starts printing escape codes. If I do not do the colorize technique, it prints blanks, just like I want it to. How can I get to behave right? The COLORIZE sourced function has the values set for $GREEN and $UNCOLOR. The list.txt has a list of servers and some other 'stuff' after the name, hence the reason for the awk.
Once it hits the end of my list of 166 current items, it prints the literal contents of the $UNCOLOR variable instead of escaping. If I don't color at all, the list prints out only the 166 items with the right output as I expect.
That worked, thank you! Now my next question is why did that work? Why specifically after a null do escape codes stop being processed, even when specifying that they are an escape code?
It has to do with how the shell command lines are parsed. When you have a variable in a shell script line, and that variable is an empty string, it adds nothing to that line at that point. How this behaves in the shell parsing depends on whether there are quotes or not. Without the quotes, the empty string will not even be an argument. With the quotes, it will be an argument, though it will be one with an empty string.
Type in this little script:
Code:
#!/bin/bash
echo Number of arguments is $#
Make it executable, then try it with various arguments:
Code:
altair/phil /home/phil 1> cat > littlescript
#!/bin/bash
echo Number of arguments is $#
altair/phil /home/phil 2> chmod +x littlescript
altair/phil /home/phil 3> ./littlescript abc xyz foo bar
Number of arguments is 4
altair/phil /home/phil 4> a=abc;b=xyz
altair/phil /home/phil 5> ./littlescript foo ${a} ${b} bar
Number of arguments is 4
altair/phil /home/phil 6> a="";b=""
altair/phil /home/phil 7> ./littlescript foo ${a} ${b} bar
Number of arguments is 2
altair/phil /home/phil 8> ./littlescript foo "${a}" ${b} bar
Number of arguments is 3
altair/phil /home/phil 9> ./littlescript foo "${a}" "${b}" bar
Number of arguments is 4
altair/phil /home/phil 10> ./littlescript foo "" "" bar
Number of arguments is 4
altair/phil /home/phil 11>
Last edited by Skaperen; 12-23-2009 at 07:40 AM.
Reason: correct paste
To answer your question more directly, it's not because the escape codes didn't work ... it's because the empty variables were omitted arguments (e.g. there was no argument at all there), and the arguments didn't match up with the formats because of the omissions in the middle. But where quotes are, the arguments won't be omitted even if they are an empty string. Quotes are also used coerce strings with spaces into one argument rather than letting the shell parse such strings into multiple arguments.
Code:
altair/phil /home/phil 11> c="foo bar"
altair/phil /home/phil 12> ./littlescript ${c}
Number of arguments is 2
altair/phil /home/phil 13> ./littlescript "${c}"
Number of arguments is 1
altair/phil /home/phil 14>
BTW, if there is a null character code (binary 0) in a string, that generally ends the string right before it. Any program written in C/C++ (as is the shell) without using specialized string handling will have that effect.
I got it, that makes sense! Because it was a null for one of the positions, it now shifts all the positions of the variables in the printf. Since every position is not a %(x)b, it prints it literal.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.