-1

I'm trying to make a CLI with cross-platform support using Python and whenever the user starts the CLI it changes the title of the terminal. In Windows, I would use title but in Linux I don't know

By the way, I use WSL (Windows Subsystem for Linux) and I need in a Python app so it has to work with os.system()

I tried asking ChatGPT but it failed instead of changing the title of the window, it change it to this:

notmithun@HPE-BOOK840:~/scripts$ echo -ne '\\033]0;My new title\\007'
\033]0;My new title\007notmithun@HPE-BOOK840:~/scripts$

TL;DR: What I want is to change the title of the Terminal window in Linux, similar to title in CMD

9
  • drop one backslash and retry! Commented Jan 30 at 15:20
  • which backslash? Commented Jan 30 at 15:23
  • on both pair: try: echo -en '\033]0;My new title\007' and maybe sleep 2 to ensure having time to read effect: echo -en '\033]0;My new title\007'; sleep 2 Commented Jan 30 at 15:25
  • I suggest echo -n $'\033]30;New Title\007'. See: superuser.com/a/1580062/340330 Commented Jan 30 at 15:26
  • @F.Hauri-GiveUpGitHub so it didn't change anything at all ``` notmithun@HPE-BOOK840:~/scripts$ echo -en '\033]0;My new title\007' notmithun@HPE-BOOK840:~/scripts$ ``` Commented Jan 30 at 15:27

1 Answer 1

1

As @F.Hauri and @Cyrus suggested, you would need an another command running. Since, I am using a Python CLI, the title doesn't go away until it returns to it's shell.

In Python, you would do:

os.system("echo -en '\033]0;My new title\007'")

Or for more options,

print("\33]0;My new title\7")

The second option is more recommend.

Thank you @F.Hauri and @Cyrus for answering this!

Sign up to request clarification or add additional context in comments.

6 Comments

If you need options then you shouldn't be using echo, use printf instead. See why-is-printf-better-than-echo.
@EdMorton printf is recommended over echo, but in python you would: print("\33]0;My new title\7") there is no need to use os.system call!!!
@Mithun Feel free to upvote comments you'd find useful
@F.Hauri-GiveUpGitHub I expect the OP is doing more than just calling echo in the os.system() call in their real code and this is just their minimal reproducible example of this specific problem.
Yes @EdMorton that is correct
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.