LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-07-2004, 04:38 AM   #1
TLV
Member
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
change console title in bash script?


Here's another bash-script question:

My default ~/.bashrc (in Debian) sets the console prompt by changing the environment variable PS1. The script also sets the console's title (showing username and pwd) by:

PROMPT_COMMAND='echo -ne "\033]0;some text here\007"'

Now, I have a shortcut on my (KDE) desktop to a script running in Konsole. I want to be able to change Konsole's title in that script. But it doesn't work using either the PS1 variable or the PROMPT_COMMAND. From the command prompt I can easily change the title using either method, just not from a script.

What am I doing wrong? How can I change the title in a script?

(I know that I can change the latter part of the title, default "Shell", by the command:
dcop #KONSOLE_DCOP_SESSION renameSession "some text"
but that's not the renaming I'm looking for.)

Thanks,
/TLV
 
Old 10-07-2004, 05:43 AM   #2
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,639

Rep: Reputation: Disabled
As I understand the mechanism, a script doesn't call a new instance of bash but is executed in the old one, so maybe here is the reason for your problem. So, I'd try to first call bash in your script and then do the rest to the new shell.

<edit>Oops, sorry, didn't read in sufficient detail. Since you are not starting from the shell maybe this is not the reason after all. Then again, it would be the login-shell...

For some reading look here.

Last edited by JZL240I-U; 10-07-2004 at 07:14 AM.
 
Old 10-07-2004, 07:10 AM   #3
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Posts: 264

Rep: Reputation: 30
OK, what if you had two scripts?

As JZL says, I think a script is opened using the currently defined title, so if the frist script simply opened Konsole, changed the name for the Konsole, and then called the second script which actually does the stuff you want?

That way, by the time the second script is called, the name has already been changed, therefore (I think) it should open with the new title?

I think if it is a global title, you could even call a third script (or add it the end of the second) to change the global title back to the defaults, so all other Konsole's don't open with the custom title.

Don't quote me on that.

 
Old 10-07-2004, 07:11 AM   #4
TLV
Member
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by JZL240I-U
As I understand the mechanism, a script doesn't call a new instance of bash but is executed in the old one, so maybe here is the reason for your problem. So, I'd try to first call bash in your script and then do the rest to the new shell.

<edit>Oops, sorry, didn't read in sufficient detail. Since you are not starting from the shell maybe this is not the reason after all.

For some reading look here.
But even if it called a new instance it wouldn't matter, right? Any instance of bash would first run ~/.bashrc, where the (typical) PS1 assignment and PROMPT_COMMAND is done. The problem being of course, that I can't change the PS1 variable in a script (other than ~/bashrc).

I'm sure there's something fundamental that I don't understand here...

/TLV
 
Old 10-07-2004, 07:17 AM   #5
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,639

Rep: Reputation: Disabled
My scheme (edited again simultaneously with your answer -> have a look ) would enable you to have a different title in the old spawning shell and in the shell just freshly spawned.
 
Old 10-07-2004, 07:19 AM   #6
TLV
Member
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 30
Here's an example-script you can try:

Code:
#!/bin/bash
PS1="\e]2;penguin\a\u@\h:\$ "
echo "Done."
If I run the PS1=.. line directly in the shell, it changes the console title to "penguin" and makes the prompt look like "username@host:$ ". However, if I run the script the console title and the prompt looks just like it did before I ran the script. So the big Q is: how do I make the script work the way I want?

/TLV
 
Old 10-07-2004, 07:27 AM   #7
TLV
Member
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by JZL240I-U
My scheme (edited again simultaneously with your answer -> have a look ) would enable you to have a different title in the old spawning shell and in the shell just freshly spawned.
I tried to have another script running the line:

/bin/bash path_to_script/title_changing_script

but it didn't work...

/TLV
 
Old 10-07-2004, 07:37 AM   #8
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,639

Rep: Reputation: Disabled
That's what I meant but did say wrong in my first post. Contrary to my assertion that a script runs in the same shell it is started from, it rather looks like it runs in a new (spawned) one. Thus it sets the variables for the new shell while you stare at your screen displying the old one from where you started the command.
 
Old 10-07-2004, 10:50 AM   #9
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
You could use an if statement in .bashrc.

export ps1 (as different variable name) from your script and then check for that variable in bashrc.

script:
export MY_NEW_PROMPT="whatever-you-want"

~.bashrc:
if [ $MY_NEW_PROMPT ]; then
PS1="$MY_NEW_PROMPT"
else
PS1="\e]2;penguin\a\u@\h:\$ "
fi
 
Old 10-07-2004, 03:18 PM   #10
TLV
Member
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by mikshaw
You could use an if statement in .bashrc.

export ps1 (as different variable name) from your script and then check for that variable in bashrc.

script:
export MY_NEW_PROMPT="whatever-you-want"

~.bashrc:
if [ $MY_NEW_PROMPT ]; then
PS1="$MY_NEW_PROMPT"
else
PS1="\e]2;penguin\a\u@\h:\$ "
fi
Nope. It doesn't work for me. Did you get it to work?

/TLV
 
Old 10-07-2004, 07:06 PM   #11
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
I have something similar in my bashrc. It's not exactly the same, but it's based on the same idea (if i understand your question correctly).

What I'm doing is setting my PS1 according to what application is being run. When I run midnight commander, I want the prompt to be shorter than usual, so I use the alias
alias mc="export mcON=1; mc -b"

Then my PS1 is
if [ "$mcON" = "1" ]; then
PS1="my_mc_prompt"
else
PS1="my_normal_prompt"
fi
 
Old 10-08-2004, 01:20 AM   #12
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
First, what is that you are trying to change, your shell prompt or your xterm/konsole titlebar/window text? The word "title" has a different meaning from the world "prompt"? PS1 has the shell prompt & PROMPT_COMMAND is something which is executed just before next the shell prompt is displayed.

If you have a bash script, then anyway upon execution you would be seeing the output of the commands in the script & not the shell prompt (unless that spawns another bash shell). Anyway, I presume you wish to change the shell prompt. My test setup is below:

Code:
SF1B:/supmis> cat .bashrc
PS1="prompt_by_.bashrc>"
SF1B:/supmis>
SF1B:/supmis> cat a
#!/usr/bin/bash

bash
SF1B:/supmis>
SF1B:/supmis> cat b
#!/usr/bin/bash

export PROMPT_COMMAND="export PS1=prompt_for_a\>"
exec a
SF1B:/supmis>
SF1B:/supmis> bash
prompt_by_.bashrc>
prompt_by_.bashrc>exit
SF1B:/supmis>
SF1B:/supmis> b
prompt_for_a>
prompt_for_a>exit
SF1B:/supmis>
SF1B:/supmis>
However if you wish to change the window title, then the dcop route may be the way to do it.

HTH.
 
Old 10-08-2004, 01:44 AM   #13
TLV
Member
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 30
mikshaw,

I liked your first solution better - unfortunately, I couldn't get either to work. Besides, hacking .bashrc for specific applications/scripts seems like overkill for me.

dustu76,

Yes, I understand if the question is a little fuzzy - and excuse my mixing of terms. What I'm trying to do is to change the konsole titlebar. This can be done using the PS1 variable (or the PROMPT_COMMAND) and the \e] sequence, so you example could apply.

However, I tried your example and it didn't work. I added some (debugging) echos in the scripts and I saw that first PROMPT_COMMAND is exported, then a is executed. When a finish (i.e., exit) any statement after "exec a" is not run. Strange.


I'm getting a little bit tired and frustraded about rhi problem. I hate it when I don't understand why something doesn't work the way my logic tells me... Oh well, there's more to life.

Can someone tell me _why_ my script in post #6 doesn't work.

Also, the dcop route only changes the "middle" part of the titlebar. The titlebar looks like "something_changed_by_\e] - Shell No. N - Konsole". dcop changes "Chell No. N".

/TLV
 
Old 10-08-2004, 01:57 AM   #14
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Dont lose heart. Its all about experimenting.

All you have to do is change the "exec a " with the script name you wish to execute. "exec" replaces the current shell with the command following it. So your current shell got replaced with "a" & that was the last command.

The modified structure of b could be :

Code:
#!/usr/bin/bash

export PROMPT_COMMAND="export PS1=prompt_for_a\>"
my_script.sh
You can fiddle with this to change your title. There is a set of tiny shell scripts which can be used to change title at http://kde-look.org/content/show.php?content=6976 I havn't used this but you can give it a try & let us know.

HTH.
 
Old 10-08-2004, 05:20 AM   #15
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,639

Rep: Reputation: Disabled
Quote:
Originally posted by TLV
...Can someone tell me _why_ my script in post #6 doesn't work...
Possibly(!) it should read
Code:
...
PS1='\e]2;penguin\a\u@\h:\$ '
...
i.e. single quotes \\' as opposed to doubles \"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
how to change user in bash script c0d3 Programming 6 07-27-2005 11:34 AM
[Req]: How to config WebSvr & DNS Svr from console by Bash shell script CiF Linux - Networking 2 03-22-2005 12:31 AM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration