Skip to content

.zshrc (maybe other?), printf fails on comments with ' -> #' #1

@RollingTom

Description

@RollingTom

When generating .zshrc using printf, hooks that contain comments
followed by a single quote (e.g. # ') cause parsing issues.
This breaks shellHook definitions and results in an invalid .zshrc.

example:

shell = "zsh";
shellHook = ''
    #'
''

Right now .zshrc gets written with printf:

printf "%s\n" \
  "export ZDOTDIR=$ZDOTDIR_" \
  'source $ZDOTDIR/.zshrc' \
  '${shellHook}' \
  '${zshHook}' \
  >$ZDOTDIR/.zshrc

Proposed fix
Write the file with a single-quoted heredoc so the body is taken literally, no interpolation, no format processing, no escaping games:

cat >"$ZDOTDIR/.zshrc" <<EOF
export ZDOTDIR=$ZDOTDIR_
source \$ZDOTDIR/.zshrc
\${shellHook}
\${zshHook}
EOF

I know this is a bit nitpicky, but it saves folks from some very confusing failures when a hook happens to contain # '.
Happy to open a PR with tests if that helps!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions