Why am I getting “too many arguments” Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Why am I getting “too many arguments” without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Here’s a part of my script that is telling me that I have too many arguments on line 3:

export LC_ALL='C'
if [ `echo "*file 2" | grep -o ^.` = '.' ]
    echo success

Anybody know why? As far as I can tell, I’m just comparing two arguments, “*” and “.”


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.

We are here to answer your question about Why am I getting “too many arguments” - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji