Skip to content

bug: awk printf rejects expression as format string #810

@chaliy

Description

@chaliy

Problem

AWK's printf requires the format string to be a string literal. When a variable or expression is used as the format argument, bashkit errors with "printf requires format string".

Reproduction

echo "my-project" | awk '{for(i=1;i<=NF;i++) printf substr($i,1,1)}'

Real awk: prints m (first char of "my-project")
Bashkit: awk: printf requires format string

Expected behavior

Per POSIX awk spec, printf accepts any string expression as the format argument:

printf expr           # expr is used as format string
printf "%s", expr     # explicit format

Both should work. The format string doesn't need to be a literal.

Workaround

# Instead of:
printf substr($i,1,1)
# Use:
printf "%s", substr($i,1,1)

Impact

Medium. The printf expr (without %s) pattern is common in awk one-liners. Used in wedow/ticket's generate_id() function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions