LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2009, 03:20 AM   #1
Rob00
LQ Newbie
 
Registered: Oct 2009
Posts: 13

Rep: Reputation: 0
Is this BASH tweak possible? (Formatting the output)


I have two questions..

Question one (easier to answer, I think):
When I edit my .bashrc file to change PS1 (bash prompt), it only changes it in gnome terminal, not in the virtual terminals (tty1,tty2, not sure the official name for them).

Why is this, and how could I make the prompt change there too?

Question two:


Is it possible to format the output in bash?

For example:

Typical Bash Output:
$ ls
Blah Blah Blah
Blah Blah Blah Blah Blah Blah
Blah Blah Blah

Formatted Bash Output:
ls
# Weee!
# Wooo!
#

or
[12:33] $ Command
[12:34] Standard output goes here!
[12:35] Woo!

As you can see, I'm inserting stuff before every output line. It might make copying output with copy/paste annoying, but sometimes I wish I could get the text a little farther away from the left side for easier reading. I'm just curious as to whether it's possible.
 
Old 10-18-2009, 04:23 AM   #2
OdinnBurkni
Member
 
Registered: Feb 2007
Location: Iceland
Distribution: Fedora 14, CentOS, FreeNAS
Posts: 127

Rep: Reputation: 20
Formatting the output

Hi.
I'm not sure about formatting the output but you can make the bashfile display some text before or after the command.
F.ex.
echo "This is describing text for the command"

When I googled format output of bash I got this among others...
http://linux.derkeiler.com/Mailing-L...4-11/0483.html
http://www.unix.com/shell-programmin...ng-output.html
http://www.faqs.org/docs/bashman/bashref_122.html
http://ubuntuforums.org/showthread.php?t=499626

Hope this will help... ...and... ...Google is your friend....
 
Old 10-18-2009, 04:24 AM   #3
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
I'll take a quick stab at the first question;

You're using a terminal emulator (konsole, xterm, eterm, whatever) that isn't launching bash with the "-l" (ell) option.

A quick work around;
1. Check your home directory for a .bash_profile file.
2. If it doesn't exist, link .bashrc to .bash_profile
Code:
ln -s ~/.bashrc ~/.bash_profile
3. if it does exist, run the following command and go over the entire output.
man bash

As for the 2nd Question, me thinks you need a quick-n-dirty introduction to the black arts of "Shell Scripting".

Have fun!

Last edited by xeleema; 10-18-2009 at 06:01 AM.
 
Old 10-18-2009, 05:04 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by xeleema View Post
2. If it doesn't exist, link .bashrc to .bash_profile
("ln -s ~/.bashrc ~/.bash_profile")
Please don't. .bashrc and .bash_profile have a different meaning: see 'man bash', the "INVOCATION" part.
 
Old 10-18-2009, 05:52 AM   #5
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
unSpawn,
Normally I wouldn't suggest symlinking the two, but as long as neither .bashrc nor .bash_profile echo messages out to the terminal (and they usually don't by default), Rob00's fine. I know one is for non-interactive login and the other for interactive login, but the short of the long is;

His session manager (xterm, eterm, konsole, whatever) is not starting bash properly, but rather than get into the dynamics of that, linking the two is fine.

Heck, the compile options for bash can be tweaked to change the default behavior.

Typically I would suggest putting the following at the bottom of the .bashrc file;

Cue the Quick-n-Dirty Intro to Shell Scripting black arts as previously mentioned....
Code:
## Are we on a terminal?
/usr/bin/tty -s
if [  $? -ne 0 ]; then
## Do we have an interactive configuration for bash?
   if [ -r ~/.bash_profile ]; then
## Source it - (Note: make sure it's sane, no printing to the term!)
      . ~/.bash_profile
   fi
fi
But I'm taking the "safe" route and suggesting the ln command rather than vi (or...emacs).

Last edited by xeleema; 10-18-2009 at 06:03 AM.
 
Old 10-18-2009, 06:05 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Bash startup files are explained here. They divide into "login/not login" and "interactive/not interactive". ~/.bashrc is for interactive shells that are not login shells. When you log in at virtual terminal it's a login shell so ~/.bashrc is not used; instead bash uses /etc/profile and the first of ~/.bash_profile, ~/.bash_login and ~/.profile that is available.

A common way to apply the same customisation to login shells as is applied to non-login interactive shells is to add this line to /etc/profile or whichever of ~/.bash_profile, ~/.bash_login or ~/.profile is used
Code:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
I favour ~/.bash_login because it is per-user rather than system-wide and the name reminds when it is used.
 
Old 10-19-2009, 12:43 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by Rob00 View Post
Question two:


Is it possible to format the output in bash?

For example:

Typical Bash Output:
$ ls
Blah Blah Blah
Blah Blah Blah Blah Blah Blah
Blah Blah Blah

Formatted Bash Output:
ls
# Weee!
# Wooo!
#

or
[12:33] $ Command
[12:34] Standard output goes here!
[12:35] Woo!

As you can see, I'm inserting stuff before every output line. It might make copying output with copy/paste annoying, but sometimes I wish I could get the text a little farther away from the left side for easier reading. I'm just curious as to whether it's possible.
there's might be a dirty way to do that in the virtual console. you can hack the vc manager (whatever it is) in the kernel to always insert a custom string on every line.
 
  


Reply



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
formatting iostat output suran Linux - General 4 07-14-2009 07:00 AM
bash - can you turn off output formatting jrfk2 Linux - General 2 01-17-2007 01:01 PM
Formatting output krock923 Programming 2 06-07-2006 07:26 AM
Formatting output of ls doodar Linux - Newbie 29 07-29-2004 01:25 PM
Bash Select Function: Formatting Output mooreted Linux - General 2 03-28-2004 06:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 PM.

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