Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.

  2. #2
    Linux 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/;//')

  3. #3
    Just 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.

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    53
    bump



    i like ice cream.

  5. #5
    Linux 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:
    Code:
    testit () {
    echo "testit"
    _col=$(stty -a | grep columns | awk '{print $7}' | sed 's/;//')
    echo $_col
    }
    Post your function please.

  6. #6
    Just 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
    
    }

  7. #7
    Linux 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.

  8. #8
    Just 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •