Results 1 to 8 of 8
how do i right align the output of the printf function from a bash script?
printf "%$[COLUMNS-4]s" "string"
for some reason, $COLUMNS has a null value when run from a ...
- 10-09-2010 #1Just Joined!
- Join Date
- Dec 2009
- Posts
- 53
printf - right alignment.
how do i right align the output of the printf function from a bash script?
printf "%$[COLUMNS-4]s" "string"
for some reason, $COLUMNS has a null value when run from a bash script. is there a way to fetch the width of the current shell session?
ty.
- 10-10-2010 #2Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
This seems to work in a script:
Code:_col=$(stty -a | grep columns | awk '{print $7}' | sed 's/;//')
- 10-10-2010 #3Just Joined!
- Join Date
- Dec 2009
- Posts
- 53
yeah it does. thanks.

just one little problem when executing inside a function, for some reason i need to specify the full path for grep. ie /bin/grep. it works fine otherwise in a functionless script.
like so:
_col=$(stty -a | /bin/grep columns | awk '{print $7}' | sed 's/;//')
is there a particular reason for that?Last edited by NiGhtMarEs0nWax; 10-11-2010 at 06:20 PM.
- 10-11-2010 #4Just Joined!
- Join Date
- Dec 2009
- Posts
- 53
bump
i like ice cream.
- 10-11-2010 #5Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
Never heard of that. I tried it in a function and it works for me. What flavor of Linux are you using? I'm using CentOS.
My function looks like this:
Post your function please.Code:testit () { echo "testit" _col=$(stty -a | grep columns | awk '{print $7}' | sed 's/;//') echo $_col }
- 10-12-2010 #6Just Joined!
- Join Date
- Dec 2009
- Posts
- 53
i am using arch.
it is a function in my bashrc for confirming that mounting a volume was successful.
$1 is a string, $2 is an exit code.
Code:function confirmation() { cols=$(stty -a | /bin/grep columns | awk '{print $7}' | sed 's/;//') strleng=`echo ${#1}` if [ $2 == 0 ]; then printf "\033[36m%s \033[1m\033[37m%s \033[32m%$[cols-$strleng-5]s" ":: " "$1" "[DONE]" else printf "\033[36m%s \033[1m\033[37m%s \033[31m%$[cols-$strleng-5]s" ":: " "$1" "[FAIL]" fi }
- 10-12-2010 #7Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
Oh, the code is in the standard personal initialization file .bashrc. I assume that during login /bin hasn't been appended to the string defined in PATH at that time for some reason
Why I don't know. I don't know how bash executes code, especially during login, and I never have used arch. So I can't help any further. You'll just have to leave it as /bin/grep.
Sorry.
- 10-13-2010 #8Just Joined!
- Join Date
- Dec 2009
- Posts
- 53
yeah it's no problem tbh, but bin is specified in PATH. i have echoed it from inside bashrc.
both awk and sed are located in bin too, which makes this problem slightly more unusual.


Reply With Quote