GeneralZero
GeneralZero

Reputation: 320

Why is this a syntax error?

I have a bash function as follows:

function folderSize{
    du -hs | awk '{print $1}'
}
folderSize

When I run it I get the following error:

./size.sh: line 2: syntax error near unexpected token `du' ./size.sh:

line 2: `   du -hs | awk "{print $1}"'

Can any one help me?

Upvotes: 1

Views: 186

Answers (3)

johnshen64
johnshen64

Reputation: 3884

function folderSize {
    du -hs $1 | awk '{print $1}'
}

folderSize '.'

Upvotes: 0

Andrew Logvinov
Andrew Logvinov

Reputation: 21851

I'm not sure if syntax of your variant is correct. I usually do it somewhat like this:

folderSize() {
  du -hs | awk '{print $1}'
}
folderSize

Upvotes: 0

Mark Reed
Mark Reed

Reputation: 95385

space is required before the {.

function folderSize {
  du -hs | awk '{print $1}'
}

Also, the way you call the function is just folderSize, no dollar sign, no parentheses.

Upvotes: 5

Related Questions