1

I'm getting an error trying to write the output of a command into a variable, which is defined in a function.

chk()
        hostsum=$(md5sum /etc/hosts | awk -F" " '{print $1}')
chk

It tells me about a syntax error:

./testchk.sh: Zeile 3: Syntaxfehler beim unerwarteten Wort hostsum=$(md5sum /etc/hosts | awk -F" " '{print $1}')' ./testchk.sh: Zeile 3: hostsum=$(md5sum /etc/hosts | awk -F" " '{print $1}')'

It works outer the function, but just won't because of the function adding some extra quotes. Any ideas except using it out of the function?

1
  • Is there a typo in your question? You need brackets (either {} or ()) on the function definition. Commented Jan 11, 2012 at 22:50

2 Answers 2

2

I think you just need to add braces:

chk() { hostsum=$(md5sum /etc/hosts | awk -F" " '{print $1}') ; }

Works fine for me here. The bash man page says that a function has to contain a compound command, of which { list ; } is one example.

Sign up to request clarification or add additional context in comments.

2 Comments

okay. this worked. thanks. but its strange, since every other function worked without the {}.
Did your other functions have one of the other compound command formats?
1

try:

chk() {
        hostsum=$(md5sum /etc/hosts | awk -F" " '{print $1}')
}
chk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.