Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
The Non-Annoying Terminal Mini How-To & Fun with shopt
View unanswered posts
View posts from last 24 hours

Goto page 1, 2  Next  
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
tomchuk
Guru
Guru


Joined: 23 Mar 2003
Posts: 317
Location: Brooklyn, NY

PostPosted: Fri Dec 05, 2003 5:46 pm    Post subject: The Non-Annoying Terminal Mini How-To & Fun with shopt Reply with quote

I recently posted this to gentoo-user and to my suprise many people had absolutely no idea how to reproduce this error, let alone how to solve it. THis is an essential piece of BASH knowledge that everyone should know.

Broken Line Wraping in Bash

What I'm talking about is the annoying habit BASH has of not checking its window size inbetween commands. To demonstrate, fire up your favorite terminal, and launch a "full screen" app like mutt, nano or less - I'll do "man bash" as it's appropriate to this How-To. Now resize your window to a larger size and quit whatever app you are using. Now try to type a long string.

Instead of:
Code:
tomchuk@dualie $cat foo | grep bar | awk this | sed that | blah, blah, blah,
blah, blah, blah, blah

you end up with:
Code:
blah, blah, blah, blahoo | grep bar | awk this | sed that | blah, blah, blah,

The line wraps onto the same line. Not very helpful.

Now try this. Open a terminal as above, and before you do anything type:
Code:
shopt -s checkwinsize

Repeat the steps you took before to induce the weird line wrap. Notice anything different? Now that's how a terminal should behave! What checkwinsize does is force BASH to check the size of the window after the completion of every command - adjusting how large it thinks the terminal is.

To make this system-wide add it to /etc/profile as root:
Code:
echo -e 'if [ "$SHELL" = "/bin/bash" ]\nthen\nshopt -s checkwinsize\nfi' >> /etc/profile

or to make sure your user has this set, add it to ~/.bashrc
Code:
echo "shopt -s checkwinsize" >> ~/.bashrc


More fun with shopt

Alright now that you have your terminal performing at an acceptable level, it's time to play with shopt a little more.

To list the options you have set to 'on':
Code:
tomchuk@dualie $ shopt -s

checkwinsize    on
cmdhist         on
expand_aliases  on
extglob         on
interactive_comments    on
login_shell     on
progcomp        on
promptvars      on
sourcepath      on

"shopt -s foo" will set option "foo" to 'on'

To list the options you have set to 'off':
Code:

thomas@dualie $ shopt -u

cdable_vars     off
cdspell         off
checkhash       off
execfail        off
histreedit      off
histappend      off
histverify      off
hostcomplete    off
huponexit       off
lithist         off
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nullglob        off
restricted_shell        off
shift_verbose   off
xpg_echo        off

"shopt -u foo" will set option "foo" to 'off'

Look at all those options we're not using! I'll cover a couple of the more interesting ones. They're all available in man bash.

cdspell
Ever type "cd /sur/src/linus" or "cd /vat/rmp/postage"? Now you can reinforce your horrible typing with a shell that corrects spelling errors in paths for you.
Code:

thomas@dualie $ cd /sur/src/linus
-bash: cd: /sur/src/linus: No such file or directory

thomas@dualie $ shopt -s cdspell

thomas@dualie $ cd /sur/src/linus
/usr/src/linux

thomas@dualie $ ls
COPYING        Makefile        arch     include  lib      security
CREDITS        README          crypto   init     mm       sound
Documentation  REPORTING-BUGS  drivers  ipc      net      usr
MAINTAINERS    System.map      fs       kernel   scripts  vmlinux


dotglob
Every want to grep through everything in a directory, or move everything including dotfiles somewhere?
Code:

thomas@dualie $ ls -lA
total 0
-rw-r--r--    1 thomas   users           0 Dec  6 13:17 .file
-rw-r--r--    1 thomas   users           0 Dec  6 13:17 file

thomas@dualie $ chmod -r *

thomas@dualie $ ls -lA
total 0
-rw-r--r--    1 thomas   users           0 Dec  6 13:17 .file
--w-------    1 thomas   users           0 Dec  6 13:17 file

thomas@dualie $ shopt -s dotglob

thomas@dualie $ chmod -r *

thomas@dualie $ ls -lA
total 0
--w-------    1 thomas   users           0 Dec  6 13:17 .file
--w-------    1 thomas   users           0 Dec  6 13:17 file

This sure beats "chmod -r .* *" as ".*" includes "." which will remove write permissions from the current directory as well as all the dotfiles in it - not good.


Last edited by tomchuk on Mon Dec 08, 2003 5:47 am; edited 4 times in total
Back to top
View user's profile Send private message
genneth
Apprentice
Apprentice


Joined: 24 Mar 2003
Posts: 152
Location: UK

PostPosted: Fri Dec 05, 2003 6:02 pm    Post subject: Reply with quote

:D:D:D:D:D:D:D:D:D:D:D:D

yes...!!!!
Back to top
View user's profile Send private message
ikokai
n00b
n00b


Joined: 10 Nov 2003
Posts: 14

PostPosted: Fri Dec 05, 2003 6:10 pm    Post subject: Reply with quote

Thanks, I always wondered how to get rid off this.
Back to top
View user's profile Send private message
marshall_j
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jan 2003
Posts: 98
Location: NZ

PostPosted: Fri Dec 05, 2003 9:38 pm    Post subject: Reply with quote

cheers for that. that always annoyed me!!
Back to top
View user's profile Send private message
twiggy
n00b
n00b


Joined: 25 Nov 2003
Posts: 65
Location: Sweden

PostPosted: Fri Dec 05, 2003 10:04 pm    Post subject: Reply with quote

yey.. that has been bugging me quite some time now, thanks :D
Back to top
View user's profile Send private message
castorilo
Apprentice
Apprentice


Joined: 25 Dec 2002
Posts: 157

PostPosted: Fri Dec 05, 2003 10:27 pm    Post subject: Reply with quote

Thanks. I was always annoyed by this and had no clue on how to reproduce this or fix it.
Back to top
View user's profile Send private message
neenee
Veteran
Veteran


Joined: 20 Jul 2003
Posts: 1786

PostPosted: Fri Dec 05, 2003 11:40 pm    Post subject: Reply with quote

thanks :wink:
Back to top
View user's profile Send private message
Unne
l33t
l33t


Joined: 21 Jul 2003
Posts: 616

PostPosted: Sat Dec 06, 2003 2:00 am    Post subject: Reply with quote

That's really great. I always thought that bash quirk was just something I had to live with.
Back to top
View user's profile Send private message
Jeedo
Apprentice
Apprentice


Joined: 02 May 2003
Posts: 202
Location: Akureyri, Iceland

PostPosted: Sat Dec 06, 2003 2:03 am    Post subject: Reply with quote

same here, thx.
Back to top
View user's profile Send private message
Halanegri
Guru
Guru


Joined: 03 Mar 2003
Posts: 351
Location: Reykjavík, Iceland

PostPosted: Sat Dec 06, 2003 2:27 am    Post subject: Reply with quote

Unne wrote:
That's really great. I always thought that bash quirk was just something I had to live with.


same here
Back to top
View user's profile Send private message
X
Apprentice
Apprentice


Joined: 30 Apr 2002
Posts: 192
Location: Lexington KY

PostPosted: Sat Dec 06, 2003 3:23 am    Post subject: Reply with quote

Happy day.
Back to top
View user's profile Send private message
verbatim
Apprentice
Apprentice


Joined: 13 Mar 2003
Posts: 223

PostPosted: Sat Dec 06, 2003 5:37 am    Post subject: Reply with quote

Very cool. :)
Back to top
View user's profile Send private message
Config
Retired Dev
Retired Dev


Joined: 25 May 2003
Posts: 187
Location: Zurich, Switzerland

PostPosted: Sat Dec 06, 2003 1:44 pm    Post subject: Reply with quote

Wow, this is just awesome - thank you a lot!
Now I can come back and enjoy pine :)
_________________
Config - caught by a chronic disease called tuxmania....
Back to top
View user's profile Send private message
grzewho
l33t
l33t


Joined: 31 Dec 2002
Posts: 626
Location: /home/g

PostPosted: Sat Dec 06, 2003 5:29 pm    Post subject: Reply with quote

this line should get to the official gentoo /etc/profile (dunno if it`s baselayout`s or bash`s file)
_________________
Code:
USE="freedom -software_patents" emerge --deep --update world
Back to top
View user's profile Send private message
viperlin
Veteran
Veteran


Joined: 15 Apr 2003
Posts: 1319
Location: UK

PostPosted: Sat Dec 06, 2003 8:30 pm    Post subject: Reply with quote

thanks that bash thing annoyed the hell out of me :lol:
Back to top
View user's profile Send private message
ikaro
Advocate
Advocate


Joined: 14 Jul 2003
Posts: 2527
Location: Denmark

PostPosted: Mon Dec 08, 2003 3:49 am    Post subject: Reply with quote

bupping this up, Thanks !
gotta love these fixes 8)
_________________
linux: #232767
Back to top
View user's profile Send private message
jesterspet
Apprentice
Apprentice


Joined: 05 Feb 2003
Posts: 215
Location: Atlanta

PostPosted: Mon Dec 08, 2003 4:42 am    Post subject: Re: The Non-Annoying Terminal Mini How-To & Fun with sho Reply with quote

tomchuk wrote:
To make this system-wide add it to /etc/profile as root


Um.. shouldn't this be added to /etc/bashrc instead of /etc/profile :?:

Since this is bash specific, and not relevant to other shells, it really doesn't belong in /etc/profile .

I cannot remember if /etc/bashrc is included in a default install, or if the check to see if it exists is included in /etc/skel/.bashrc (or ~/.bashrc) so just for the sake of completeness, I'll point out that copying & editing the one that checks for /etc/profile in ~/.bashrc works just fine.
_________________
(X) Yes! I am a brain damaged lemur on crack, and would like to buy your software package for $499.95
Back to top
View user's profile Send private message
tomchuk
Guru
Guru


Joined: 23 Mar 2003
Posts: 317
Location: Brooklyn, NY

PostPosted: Mon Dec 08, 2003 6:06 am    Post subject: Re: The Non-Annoying Terminal Mini How-To & Fun with sho Reply with quote

Glad everyone is enjoying this :D

jesterspet wrote:
Um.. shouldn't this be added to /etc/bashrc instead of /etc/profile :?:


Good catch, thanks.
I agree it shouldn't be in /etc/profile as it was, but I don't believe that gentoo's bash install looks at /etc/bashrc - it's not mentioned in the man page and I couldn't make it happen, short of adding "source /etc/bashrc" to my ~/.bashrc which defeats the purpose. I added an if statement that checks $SHELL before issueing shopt which does the trick.
Back to top
View user's profile Send private message
Krigare
Tux's lil' helper
Tux's lil' helper


Joined: 12 Nov 2003
Posts: 92
Location: ::1

PostPosted: Mon Dec 08, 2003 9:06 am    Post subject: Reply with quote

this is just perfect :)
_________________
Together we are strong.
Back to top
View user's profile Send private message
tarzan420
Tux's lil' helper
Tux's lil' helper


Joined: 05 Jul 2003
Posts: 81
Location: Fairbanks AK

PostPosted: Wed Dec 10, 2003 5:58 pm    Post subject: Screwy Word Wrap because of ANSI Colors? Reply with quote

I had gotten the impression that the word wrap problems had to do with ANSI colors in the bash profile (You know, green/red username, blue path). I came to this conclusion after I read more about them in a recent GWN, and then tried to use the colors for my prompt on my debian box. While the color worked, I still had strange line wrapping problems. Cause and Effect? I'm not sure.
Back to top
View user's profile Send private message
tomchuk
Guru
Guru


Joined: 23 Mar 2003
Posts: 317
Location: Brooklyn, NY

PostPosted: Wed Dec 10, 2003 8:00 pm    Post subject: Reply with quote

Like you said, weird line wraps can also be caused by improperly escaped prompts (PS1). For every non-printing character (color codes, etc) you must escape them with \[ and \].

A simple colored PS1:
Code:
PS1='\033[01;34m\u@\033[01;31m\h \$'

That will cause weird line wrapping because when calculating the window width and the length of the prompt bash considers the color codes as priniting characters. The solution is to escape the color codes like:
Code:
PS1='\[\033[01;34m\]\u@\[\033[01;31m\]\h \$'


Try each of those and you'll find that the first will wrap the line weirdly but the second is just fine.
Back to top
View user's profile Send private message
OneOfOne
Guru
Guru


Joined: 28 May 2003
Posts: 368

PostPosted: Sat Dec 13, 2003 7:47 pm    Post subject: Reply with quote

awesome!
Back to top
View user's profile Send private message
rhill
Retired Dev
Retired Dev


Joined: 22 Oct 2004
Posts: 1629
Location: sk.ca

PostPosted: Sun Nov 28, 2004 1:05 pm    Post subject: Reply with quote

bump.
_________________
by design, by neglect
for a fact or just for effect
Back to top
View user's profile Send private message
SaFrOuT
Apprentice
Apprentice


Joined: 08 Jul 2003
Posts: 256
Location: Egypt

PostPosted: Sun Nov 28, 2004 2:47 pm    Post subject: Reply with quote

although it is an old thread but it was very helpful

thanks dirtyepic for bumbing it

and SURE thanks tomchuk
Back to top
View user's profile Send private message
zsoltika
l33t
l33t


Joined: 13 Nov 2003
Posts: 634
Location: Budapest, Hungary

PostPosted: Mon Nov 29, 2004 6:17 am    Post subject: Re: The Non-Annoying Terminal Mini How-To & Fun with sho Reply with quote

Hi guys,
I followed this steps:
tomchuk wrote:

Broken Line Wraping in Bash
What I'm talking about is the annoying habit BASH has of not checking its window size inbetween commands. To demonstrate, fire up your favorite terminal, and launch a "full screen" app like mutt, nano or less - I'll do "man bash" as it's appropriate to this How-To. Now resize your window to a larger size and quit whatever app you are using. Now try to type a long string.
Instead of:
Code:
tomchuk@dualie $cat foo | grep bar | awk this | sed that | blah, blah, blah,
blah, blah, blah, blah

you end up with:
Code:
blah, blah, blah, blahoo | grep bar | awk this | sed that | blah, blah, blah,

The line wraps onto the same line. Not very helpful.
Now try this. Open a terminal as above, and before you do anything type:
Code:
shopt -s checkwinsize

Repeat the steps you took before to induce the weird line wrap. Notice anything different? Now that's how a terminal should behave! What checkwinsize does is force BASH to check the size of the window after the completion of every command - adjusting how large it thinks the terminal is.
To make this system-wide add it to /etc/profile as root:
Code:
echo -e 'if [ "$SHELL" = "/bin/bash" ]\nthen\nshopt -s checkwinsize\nfi' >> /etc/profile

or to make sure your user has this set, add it to ~/.bashrc
Code:
echo "shopt -s checkwinsize" >> ~/.bashrc


but after this I still end up whit messed up command line :(
I mean, using this
Code:
export PS1="[\e[31m\$(pwd)\e[0m :: \e[32m\@\e[0m :: \e[33m\l\e[0m]\n[\e[32m\u\e[
0m@\e[34m\H\e[0m] \$ "
I still end up with command lines like this:
Code:
aaaaaaaaaaaaaaaaaaaaaaa'ss prova_a0.svg | grep 'aaaaaaaaaaaaaa

I'm using Xterm(196) and GNU bash, version 3.00.13(1)-release (i686-pc-linux-gnu).
Any idea?
Thx,
Zsoltika
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum