Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

`set_key` does not handle single quote `'` inside value properly

Đang mở
#543 1 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Start at the set_key entry point and reproduce the issue with the Python script in this report, then source the generated .env file in Bash. Trace how values are quoted and escaped, including the related PR discussion and warning about dotenv parsing. Done means a value containing a single quote is written in a form that both Bash can source and dotenv can parse correctly.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Problem

set_key function does not properly handle single quote ' inside value.

python-dotenv version: 1.0.1

Related PR

How to reproduce

  1. Run python script below
  2. Run source .env

Python Script

from dotenv import set_key

def main():
    var_value = "I'm a bug :("
    set_key(".env", "VAR", var_value)


if __name__ == "__main__":
    main()

Error from source .env

bash: .env: line 1: syntax error near unexpected token `('
bash: .env: line 1: `VAR='I\'m a bug :(''

Example of fix

⚠️ this fix produces values that dotenv cannot parse (see this issue) ⚠️

from dotenv import set_key

def fixed_set_key(file_path, key, value):
    # properly escape ' if present
    # https://stackoverflow.com/questions/8254120/how-can-i-escape-a-single-quote-in-a-single-quote-string-in-bash/26165123#26165123
    value = value.replace("'", "'\"'\"'")
    
    set_key(file_path, key, f"'{value}'", quote_mode="never")

def main():
    var_value = "I'm a fixed :)"
    fixed_set_key(".env", "VAR", var_value)


if __name__ == "__main__":
    main()
Ngôn ngữ chính
Python
Star
8.9k
Fork
581
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của theskumar/python-dotenv

Tất cả issue của theskumar/python-dotenv

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.