Find the answer to your Linux question:
Results 1 to 8 of 8
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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?


  2. #2
    This seems to work in a script:

    _col=$(stty -a | grep columns | awk '{print $7}' | sed 's/;//')

  3. #3
    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. $spacer_open
  5. #4

    i like ice cream.

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

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

    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]"
    		printf "\033[36m%s \033[1m\033[37m%s \033[31m%$[cols-$strleng-5]s" ":: " "$1" "[FAIL]"

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


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