How to trim whitespace from a Bash variable?

后端 未结 30 2227
星月不相逢
星月不相逢 2020-11-22 06:09

I have a shell script with this code:

var=`hg st -R \"$path\"`
if [ -n \"$var\" ]; then
    echo $var
fi

But the conditional code always ex

相关标签:
30条回答
  • 2020-11-22 06:36

    To remove spaces and tabs from left to first word, enter:

    echo "     This is a test" | sed "s/^[ \t]*//"
    

    cyberciti.biz/tips/delete-leading-spaces-from-front-of-each-word.html

    0 讨论(0)
  • 2020-11-22 06:37

    This is the simplest method I've seen. It only uses Bash, it's only a few lines, the regexp is simple, and it matches all forms of whitespace:

    if [[ "$test" =~ ^[[:space:]]*([^[:space:]].*[^[:space:]])[[:space:]]*$ ]]
    then 
        test=${BASH_REMATCH[1]}
    fi
    

    Here is a sample script to test it with:

    test=$(echo -e "\n \t Spaces and tabs and newlines be gone! \t  \n ")
    
    echo "Let's see if this works:"
    echo
    echo "----------"
    echo -e "Testing:${test} :Tested"  # Ugh!
    echo "----------"
    echo
    echo "Ugh!  Let's fix that..."
    
    if [[ "$test" =~ ^[[:space:]]*([^[:space:]].*[^[:space:]])[[:space:]]*$ ]]
    then 
        test=${BASH_REMATCH[1]}
    fi
    
    echo
    echo "----------"
    echo -e "Testing:${test}:Tested"  # "Testing:Spaces and tabs and newlines be gone!"
    echo "----------"
    echo
    echo "Ah, much better."
    
    0 讨论(0)
  • 2020-11-22 06:38

    A simple answer is:

    echo "   lol  " | xargs
    

    Xargs will do the trimming for you. It's one command/program, no parameters, returns the trimmed string, easy as that!

    Note: this doesn't remove all internal spaces so "foo bar" stays the same; it does NOT become "foobar". However, multiple spaces will be condensed to single spaces, so "foo bar" will become "foo bar". In addition it doesn't remove end of lines characters.

    0 讨论(0)
  • 2020-11-22 06:38

    In order to remove all the spaces from the beginning and the end of a string (including end of line characters):

    echo $variable | xargs echo -n
    

    This will remove duplicate spaces also:

    echo "  this string has a lot       of spaces " | xargs echo -n
    

    Produces: 'this string has a lot of spaces'

    0 讨论(0)
  • 2020-11-22 06:38

    Here's a trim() function that trims and normalizes whitespace

    #!/bin/bash
    function trim {
        echo $*
    }
    
    echo "'$(trim "  one   two    three  ")'"
    # 'one two three'
    

    And another variant that uses regular expressions.

    #!/bin/bash
    function trim {
        local trimmed="$@"
        if [[ "$trimmed" =~ " *([^ ].*[^ ]) *" ]]
        then 
            trimmed=${BASH_REMATCH[1]}
        fi
        echo "$trimmed"
    }
    
    echo "'$(trim "  one   two    three  ")'"
    # 'one   two    three'
    
    0 讨论(0)
  • 2020-11-22 06:38

    Python has a function strip() that works identically to PHP's trim(), so we can just do a little inline Python to make an easily understandable utility for this:

    alias trim='python -c "import sys; sys.stdout.write(sys.stdin.read().strip())"'
    

    This will trim leading and trailing whitespace (including newlines).

    $ x=`echo -e "\n\t   \n" | trim`
    $ if [ -z "$x" ]; then echo hi; fi
    hi
    
    0 讨论(0)
提交回复
热议问题