[: too many arguments means a variable inside [ ] held a value
with a space and was not quoted, so the test received extra words. Write
[ "$name" = "admin" ] with double quotes and the value stays one word.
The script carried on: the if went to its else branch and the
script exited with 0.
The error
The two-word value was passed from a small runner script, so that no shell on the way could change its quotes:
docker exec fix10-lc-u3 bash -c 'cat /root/role.sh; cat /root/twowords.sh'
#!/bin/bash
name="$1"
if [ $name = "admin" ]; then
echo "welcome, admin"
else
echo "not admin"
fi
cd /root
bash role.sh 'Maria Garcia'
echo exit=$?
bash -x role.sh 'Maria Garcia'
docker exec fix10-lc-u3 bash -c 'bash /root/twowords.sh 2>&1'
role.sh: line 3: [: too many arguments
not admin
exit=0
+ name='Maria Garcia'
+ '[' Maria Garcia = admin ']'
role.sh: line 3: [: too many arguments
+ echo 'not admin'
not admin
Why it happens
The argument was Maria Garcia, one value with a space. Assigned with quotes,
name="$1" kept it whole; the unquoted $name on line 3 did not.
The bash -x trace in the second half of the output shows the words
[ received: Maria, Garcia, = and
admin, four instead of three.
The output looks correct by accident: Maria Garcia is not the admin, and the script
printed not admin. It printed that only because the test failed, not
because it compared the name.
The fix
docker exec fix10-lc-u3 bash -c 'cat /root/twowords-fix.sh'
cd /root
sed -n 3p role2.sh
bash role2.sh 'Maria Garcia'
echo exit=$?
docker exec fix10-lc-u3 bash /root/twowords-fix.sh
if [ "$name" = "admin" ]; then
not admin
exit=0
role2.sh is the same script with $name in double quotes on
line 3. The error line is gone and the comparison now runs on the whole value, so the
not admin answer is now the result of a real comparison. If a longer script
fails this way, run it with bash -x as above to see which variable split
into several words. An empty value breaks the unquoted line in a different way; that case
has its own post, linked below.
How it was reproduced
The capture was made in an ubuntu:24.04 container (Ubuntu 24.04.5 LTS, bash
5.2.21) on Docker Engine 29.9.3, started with
docker run -d --name fix10-lc-u3 ubuntu:24.04 sleep infinity. The scripts were
copied in with docker cp (for example
docker cp l3\twowords.sh fix10-lc-u3:/root/twowords.sh). Every command was
sent from Windows PowerShell 5.1 as shown, with any bash -c text in single
quotes so it reached the container unchanged. The container name is the test's own, and
Maria Garcia is a sample name.
Frequently asked
- What does [: too many arguments mean in bash?
- A [ ] test got more words than it expected, here from an unquoted variable holding Maria Garcia. bash -x showed four words where three were meant.
- How do I fix too many arguments in a bash if statement?
- Quote the variable: [ "$name" = "admin" ]. In the test the quoted line ran without an error for the two-word value.
- Why does my bash script keep running after too many arguments?
- The failed test counts as false, so the if took its else branch. In the test the script printed not admin and exited with 0.
More decoded errors in the Fixes category. The empty value case is [: =: unary operator expected.