Linux – Why does the Bash script display the “too many arguments” error at the cp command

argumentsbashcplinuxscript

Here is my script, I get the error "line 33: [: too many arguments", I'm confused why, surely only 2 arguments are being provided to cp here?

I am providing two directories to the script with no spaces in them, i.e. $1=dir1/ and $2=dir2/

#!/bin/bash

### Assign suitable names to arguements. ###

source=$1
dest=$2

### Error handler for all script errors. ###

function errorHandler {
    case $1 in
        ERRargs) printf "USAGE: e2backup source_dir dest_dir.\n"; exit 1;;
        ERRsource) printf "ERROR: Source does not exist or is not a directory.\n"; exit 2;;
        ERRdest) printf "ERROR: Destination does not exist or is not a directory.\n"; exit 3;;
        ERRempty) printf "ERROR: Destination is not empty.\n"; exit 4;;
    esac
}

### Test num. of args, source/dest validity and empty dest. Then perform backup. ###

if [ $# -ne 2 ]
  then
    errorHandler ERRargs
elif [ ! -d $source ]
  then
    errorHandler ERRsource
elif [ ! -d $dest ]
  then
    errorHandler ERRdest
elif [ -n "$(ls -A $dest)" ] 
  then
    errorHandler ERRempty
elif [ cp -R $source $dest ]
  then
    printf "Successfully backed-up from $source to $dest"; exit
else
    printf "Back-up failed, please see e2backup.error"; exit 5
fi

Best Answer

It is not cp but [ aka test which gives you the error

Related Question