I have this set of commands :

s3ls="aws s3 ls $ssyss3/ --recursive --human-readable --summarize"
egrepCommand="egrep '$currentDate|Total'"
totalSize=$(echo $s3ls|$egrepCommand| awk -F 'Total Size:' '{print $2}'|sed '/^$/d')
echo $totalSize

but I'm getting this error :

egrep '2019-05-11|Total': command not found

What am I missing?


As it says on the tin. You create a command name which is egrep '2019-05-11|Total' and then try to call it. This is not a egrep followed by a parameter but a complete command name. What you want is more likely:

totalSize=$(echo $s3ls| egrep "$currentDate|Total" | awk -F 'Total Size:' '{print $2}'|sed '/^$/d')

If necessary you can use a variable to hold the parameter:

totalSize=$(echo $s3ls| egrep "$egrepParm" | awk -F 'Total Size:' '{print $2}'|sed '/^$/d')

Some will say that in a script it is best to use grep -E instead of egrep

PS: An aliased command will work in the command prompt/terminal but not in a script, so trying the command in the terminal isn't proof that it will work in a script. In bash, use type {commandname} to check if the name is a real command or an alias. egrep can be implemented as an alias in some Unix versions, in my Ubuntu it is a plain command.

  • You can store the command, but you must use an array: cmd=( grep -E "$currentDate|Total" ) then echo "$s3ls" | "${cmd[@]}" | ... May 14 '19 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.