Why am I getting "too many arguments"
echo "*file 2" | grep -o ^.
prints *
.
Since you have a command substitution outside double quotes, it undergoes globbing (a.k.a. wildcard matching a.k.a. filename generation) and word splitting. If the current directory is not empty, *
expands to the list of files in the current directory. Each file becomes one token in the [
command, which is highly likely to be a syntax error.
The problem is that you didn't use double quotes around the command substitution. Always use double quotes around variable and command substitutions unless you have a good reason to omit them.
if [ "$(echo "*file 2" | grep -o ^.)" = '.' ]
See Why does my shell script choke on whitespace or other special characters? for a more detailed explanation.
Related videos on Youtube
Aaron
Updated on September 18, 2022Comments
-
Aaron almost 2 years
Here's a part of my script that is telling me that I have too many arguments on line 3:
#!/bin/bash export LC_ALL='C' if [ `echo "*file 2" | grep -o ^.` = '.' ] then echo success fi
Anybody know why? As far as I can tell, I'm just comparing two arguments, "*" and "."
-
Aaron over 9 yearsThanks, I'm still new to shell scripting, so all help is appreciated!
-
FantomX1 over 3 yearssingle quotes had the same problem though and normally they seemed to work