I've written the following bash function. It is supposed to print a command, run it and print its output, and print a short description.
#!/bin/bash
print_and_run_command() {
COMMAND="$1"
DESCRIPTION="$2"
OUTPUT="`$COMMAND`"
printf "%-30s %-30s %s\n" "$COMMAND" "$OUTPUT" "$DESCRIPTION"
}
print_and_run_command "date +%S" "Second"
print_and_run_command" date +%H" "Hour"
print_and_run_command "date +%Y-%m-%dT%H:%M:%S%z" "T-separated rfc 3339 / ISO 8601"
Each of the date commands works well when executed from a shell. However, when running the script, a strange error happens:
date +%S 34 Second
./blah.sh: line 11: print_and_run_command date +%H: command not found
date +%Y-%m-%dT%H:%M:%S%z 2013-07-30T14:20:34-0700 T-separated rfc 3339 / ISO 8601
Running date +%H
fails, even though date +%S
and the complicated date +%Y-%m-%dT%H:%M:%S%z
work just fine.
Any ideas what is failing the print_and_run_command" date +%H" "Hour"
?