Bash [: too many arguments more than a character

This is not a question (although I have one at the end), but rather a solution to a problem that I would like to share if it helps someone else.

For the longest time, I got bash: [: too many argumentswhen opening a new terminal (in particular, iTerm2 on OS X with the bash -completion macport macro installed). This error occurred from a line if [ -n "$BASH_VERSION" -a -n "$PS1" -a -z "$BASH_COMPLETION_COMPAT_DIR" ]; thenin the file /opt/local/etc/bash_completion. I finally found a problem in what is export PS1='>'in mine .bash_profile. Changing PS1 to something else (for example '> ') fixes the issue with terminating bash.

Some experiments in OS X and Debian show that this problem occurs when additional expressions (with -aor -o) are added to test ( [ ]) after an expression that includes '>'. For example.

> A='>'; if [ -n "$A" ]; then echo "yes"; fi
yes
> A='>'; if [ -n "$A" -a -n "$A" ]; then echo "yes"; fi
bash: [: too many arguments
> A='> '; if [ -n "$A" -o -n "$A" ]; then echo "yes"; fi
yes
> A='>'; if [ -n "$A" -o -n "Hello" ]; then echo "yes"; fi
bash: [: too many arguments
> A='>'; if [ -n "Hello" -a -n "$A" ]; then echo "yes"; fi
yes

Is this a (known) bug in bash?

+4
source share
2 answers

Your workaround is effective if the line stored in $Ais not an operator that recognizes [/ test- just adding a space is enough, as you discovered.

Is "more than" to be interpreted as just a string? It works with '>' in the end.

, $A . ( , [[ , , .)

[ (test) ( ) , :

  • - $A .
  • [

, [, , , - > , .

, : > ( ) ; >, , ><space> , , .


:

XSI, -a -o, '(' ') . ( , , .)

  • , [ ... ], && ( -a) || ( -o), :

    [ -n "$BASH_VERSION" ] && [ -n "$PS1" ] && [ -z "$BASH_COMPLETION_COMPAT_DIR" ]
    

, , , true:

[ "$BASH_VERSION" ] && [ "$PS1" ] && [ -z "$BASH_COMPLETION_COMPAT_DIR" ]

, -a -o , - .

+4

,

if [ condition1 ] && [condition2 ]

if [ condition1 ] || [condition2 ]

(, "" ):

A='>'; if [ -n "$A" ] && [ -n "$A" ]; then echo "yes"; fi

"" if:

A='>'; if [ -n "$A" ] || [ -n "Hello" ]; then echo "yes"; fi

, [ -n "Hello" ] , .

shellcheck, bash script.

+2

Source: https://habr.com/ru/post/1630067/


All Articles