Search code examples
bashshellprintfquoting

printf is using my files in the directory the script is called from


I have a line of code printf "%5d%s%s%d%s" $opNum $IDLE_TEXT $LINE_ENDER $ops "." All the variables passed are defined, but for some ungodly reason, printf is trying to grab files in the dir the script is called from.

Example of error output:

./asg4.sh: line 248: printf: alpha.sh: invalid number 9:1243210asg4.sh

Anyone have any idea what is going on here?


Solution

  • If your intention is to have a . do it as part of the output, do it as part of the <FORMAT> section rather than part of being in <ARGUMENTS...> section, when you think printf works as

    printf <FORMAT> <ARGUMENTS...>
    

    So adding . in the former section

    printf "%5d%s%s%d." "$opNum" "$IDLE_TEXT" "$LINE_ENDER" "$ops"
    #               ^^^  Adding the . to the <format> section
    

    and double-quote all your shell variables, to avoid word-splitting by the default field-separator in shell.


    From the comments below, it did seem because of the lack of double-quotes in the arguments was the actual reason for the error. In your case * trying to expand the folder contents.